• taro3.x: webSocket简单实现


    封装组件使用,只创建一个对象:

    import Taro from '@tarojs/taro'
    
    class CustomSocket {
        public socketOpen: boolean = false
        public socketMsgQueue: string[] = []
        private lockReConnect: boolean = false
        private timer: any = null
        private limit: number = 0
        constructor() {
            this.connectSocket()
            console.log('init CustomSocket')
        }
        connectSocket() {
            const _this = this
            Taro.connectSocket({
                url: `wss://api.fczx.com/wss?userToken=${1}`,
                success: (response: any) => {
                    console.log('connectSocket success:', response)
                    _this.initSocketEvent()
                },
                fail: (e: any) => {
                    console.log('connectSocket fail:', e)
                }
            })
        }
    
        initSocketEvent() {
            const _this = this
            Taro.onSocketOpen(() => {
                console.log('onSocketOpen')
                _this.socketOpen = true
                for (const item of _this.socketMsgQueue) {
                    _this.sendSocketMessage(item)
                }
                _this.socketMsgQueue = []
            })
            Taro.onSocketError((e: any) => {
                console.log("WebSocket error", e)
            })
            Taro.onSocketClose(() => {
                console.log('WebSocket close!')
                _this.reconnectSocket()
            })
        }
    
        reconnectSocket() {
            const _this = this
            if (_this.lockReConnect) {
                return
            }
            _this.lockReConnect = true
            clearTimeout(_this.timer)
            if (_this.limit < 10) {
                _this.timer = setTimeout(() => {
                    _this.connectSocket()
                    _this.lockReConnect = false
                }, 5000)
                _this.limit = _this.limit + 1
            }
        }
    
        public sendSocketMessage(messgage: string, errorCallback: (any) => void = () => { }) {
            const _this = this
            if (_this.socketOpen) {
                Taro.sendSocketMessage({
                    data: messgage,
                    success: () => {
                        console.log('sendSocketMessage succ', messgage)
                    },
                    fail: (e: any) => {
                        console.log('sendSocketMessage fail', e)
                        errorCallback && errorCallback(true)
                    }
                })
            } else {
                _this.socketMsgQueue = []
            }
        }
    
        public onSocketMessage(callback: (string) => void) {
            Taro.onSocketMessage((response: any) => {
                const message: any = JSON.parse(response.data)
                if (message.type === 'chat') {
                    callback(message)
                }
                console.log('onSocketMessage:', message)
            })
        }
    
        public closeSocket() {
            if (this.socketOpen) {
                Taro.closeSocket()
            }
        }
    
    }
    
    export default new CustomSocket()
  • 相关阅读:
    SQL Server 之 在与SQLServer建立连接时出现与网络相关的或特定于实例的错误
    T-SQL 之 概述
    HTML 之 Web页面表单form中只有一个input的text元素,按回车默认提交
    CollatingOfData 之 JsonHelper
    JavaScript 之 弹出窗口总结
    JavaScript 之 页面跳转及Frame动态加载
    XML 之 与Json或String的相互转换
    UML 之 类图(Class Dragram)
    Asp.Net 之 调用分享接口
    echo print() print_r() var_dump()的区别
  • 原文地址:https://www.cnblogs.com/Nyan-Workflow-FC/p/14158263.html
Copyright © 2020-2023  润新知