uniapp使用websocket

openSocket() {
                //打开时的动作
                uni.onSocketOpen(() => {
                    console.log('WebSocket 已连接')
                    this.socketStatus = 'connected'
                    uni.showToast({
                        title: '实时推送已连接',
                        icon: 'none'
                    })
                })
                //断开时的动作
                uni.onSocketClose(() => {
                    this.socketStatus = 'closed'
                    console.log('WebSocket 已断开')
                    uni.showToast({
                        title: '实时推送已断开连接',
                        icon: 'none'
                    })
                })
                //报错时的动作
                uni.onSocketError(error => {
                    console.error('socket error:', error)
                })
                // 监听服务器推送的消息
                uni.onSocketMessage((message: any) => {
                    console.log(11111111,message)

                })
                // 打开信道
                uni.connectSocket({
                    url: 'ws://192.168.1.124:8086/s/web_socket'
                });
            },

 

posted @ 2021-09-24 11:17  赵永强  阅读(2528)  评论(1)    收藏  举报