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

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

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

    腾讯云最新优惠活动来了:云产品限时1折,云服务器低至88元/年 ,点击这里立即抢购:9i0i.cn/qcloud,更有2860元代金券免费领取,付款直接抵现金用,点击这里立即领取:9i0i.cn/qcloudquan

    (福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

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

    10 条回复  ?  2023-11-30 09:30:39 +08:00
    HoseaDev
        1
    HoseaDev  
       164 天前
    1.文档。
    2.GPT 。
    tlerbao
        2
    tlerbao  
    OP
       164 天前
    @HoseaDev 文档那是一个简单,GPT 表示不会或者信息过时了。
    webluoye
        4
    webluoye  
       164 天前
    google 的 bard 可以回复
    Puteulanus
        5
    Puteulanus  
       164 天前
    把下面的 source 给 ChatGPT 让它重写了文档和示例,你看能用不 https://chat.openai.com/share/1a3f76ba-40ab-48ae-be95-d3107234ae7a
    lx000377
        6
    lx000377  
       164 天前
    写的贼烂...
    tlerbao
        7
    tlerbao  
    OP
       164 天前
    @lx000377
    小哥 原生中 ws.onopen 里有一些操作,对应 useWebSockt 应该写在哪里?
    ```
    ws = new WebSocket(url);
    ws.onopen = function () {
    m_isConnectWS = true;
    unload();
    sendInitMsg(); //初始化
    sendGetPath(); //获取电脑中的路径
    m_closed = false;
    };
    ```
    Dragonphy
        8
    Dragonphy  
       163 天前
    GPT 不会你可以把源码实现塞给他
    xujiahui
        9
    xujiahui  
       163 天前
    文档不是写的挺清楚吗
    xujiahui
        10
    xujiahui  
       163 天前
    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   ·   实用小工具   ·   960 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 21:12 · PVG 05:12 · LAX 14:12 · JFK 17:12
    Developed with CodeLauncher
    ? Do have faith in what you're doing.


    http://www.vxiaotou.com