V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
tlerbao
V2EX  ›  程序员

用过 VueUse 中 useWebSocket 的前端小哥麻烦丢点代码示例

  •  
  •   tlerbao · 150 天前 · 1323 次点击
    这是一个创建于 150 天前的主题,其中的信息可能已经有所发展或是发生改变。

    useWebSocket 麻烦前端小哥丢点代码示例,不太会用哈。

    10 条回复    2023-11-30 09:30:39 +08:00
    HoseaDev
        1
    HoseaDev  
       150 天前
    1.文档。
    2.GPT 。
    tlerbao
        2
    tlerbao  
    OP
       150 天前
    @HoseaDev 文档那是一个简单,GPT 表示不会或者信息过时了。
    webluoye
        4
    webluoye  
       150 天前
    google 的 bard 可以回复
    Puteulanus
        5
    Puteulanus  
       150 天前
    把下面的 source 给 ChatGPT 让它重写了文档和示例,你看能用不 https://chat.openai.com/share/1a3f76ba-40ab-48ae-be95-d3107234ae7a
    lx000377
        6
    lx000377  
       150 天前
    写的贼烂...
    tlerbao
        7
    tlerbao  
    OP
       150 天前
    @lx000377
    小哥 原生中 ws.onopen 里有一些操作,对应 useWebSockt 应该写在哪里?
    ```
    ws = new WebSocket(url);
    ws.onopen = function () {
    m_isConnectWS = true;
    unload();
    sendInitMsg(); //初始化
    sendGetPath(); //获取电脑中的路径
    m_closed = false;
    };
    ```
    Dragonphy
        8
    Dragonphy  
       149 天前
    GPT 不会你可以把源码实现塞给他
    xujiahui
        9
    xujiahui  
       149 天前
    文档不是写的挺清楚吗
    xujiahui
        10
    xujiahui  
       149 天前
    export interface UseWebSocketReturn<T> {
    /**
    * Reference to the latest data received via the websocket,
    * can be watched to respond to incoming messages
    */
    data: Ref<T | null>

    /**
    * The current websocket status, can be only one of:
    * 'OPEN', 'CONNECTING', 'CLOSED'
    */
    status: Ref<WebSocketStatus>

    /**
    * Closes the websocket connection gracefully.
    */
    close: WebSocket['close']

    /**
    * Reopen the websocket connection.
    * If there the current one is active, will close it before opening a new one.
    */
    open: Fn

    /**
    * Sends data through the websocket connection.
    *
    * @param data
    * @param useBuffer when the socket is not yet open, store the data into the buffer and sent them one connected. Default to true.
    */
    send: (data: string | ArrayBuffer | Blob, useBuffer?: boolean) => boolean

    /**
    * Reference to the WebSocket instance.
    */
    ws: Ref<WebSocket | undefined>
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   879 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 21:24 · PVG 05:24 · LAX 14:24 · JFK 17:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.