PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > HBuilderX教程 > 正文

    uniapp使用websocket

    作者:admin来源:网络浏览:时间:2021-09-28 11:09:11我要评论
    导读:uniapp使用websocket
    uniapp使用websocket

    1. openSocket() { 
    2.                 //打开时的动作 
    3.                 uni.onSocketOpen(() => { 
    4.                     console.log('WebSocket 已连接'
    5.                     this.socketStatus = 'connected' 
    6.                     uni.showToast({ 
    7.                         title: '实时推送已连接'
    8.                         icon: 'none' 
    9.                     }) 
    10.                 }) 
    11.                 //断开时的动作 
    12.                 uni.onSocketClose(() => { 
    13.                     this.socketStatus = 'closed' 
    14.                     console.log('WebSocket 已断开'
    15.                     uni.showToast({ 
    16.                         title: '实时推送已断开连接'
    17.                         icon: 'none' 
    18.                     }) 
    19.                 }) 
    20.                 //报错时的动作 
    21.                 uni.onSocketError(error => { 
    22.                     console.error('socket error:', error) 
    23.                 }) 
    24.                 // 监听服务器推送的消息 
    25.                 uni.onSocketMessage((message: any) => { 
    26.                     console.log(11111111,message) 
    27.  
    28.                 }) 
    29.                 // 打开信道 
    30.                 uni.connectSocket({ 
    31.                     url: 'ws://192.168.1.124:8086/s/web_socket' 
    32.                 }); 
    33.             }, 

     

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-146-6634-1.html
    相关热词搜索: uniapp websocket