Vue里怎么使用socket?(websocket)

本文介绍了如何在Vue应用中封装和使用WebSocket,步骤包括创建WebSocket实例、事件监听和消息发送。重点展示了如何通过Socket.js处理socket连接、消息传递及错误处理。

第一步:封装socket.js

import { Message } from 'iview';

class Socket {
        constructor (wsurl, binaryType){
                this.wsurl = wsurl
                this.websock = null
                this.binaryType = binaryType
                this.initWebSocket()
        }
        initWebSocket(){
                var that = this
                this.websock = new WebSocket(this.wsurl)
                if(this.binaryType){
                        this.websock.binaryType = this.binaryType
                }
                this.websock.onmessage = function(e){
                        that.webMessage(e)
                }
                this.websock.onclose = function(e){
                        that.webClose(e)
                }
                this.websock.onopen = function(){
                        that.webOpen()
                }
                this.websock.onerror = function(){
                        Message.error('websocket连接中断!请刷新重试')
                }
        }
        sendSock(agentData, callback){
                this.callback = callback
                if(agentData){
                        if(this.websock.readyState === this.websock.OPEN){
                                this.webSend(agentData)
                        } else if(this.websock.readyState === this.websock.CONNECTING){
                                let that = this
                                setTimeout(function(){
                                        that.webSend(agentData)
                                }, 300)
                        }else{
                                this.initWebSocket()
                                let that = this
                                setTimeout(function(){
                                        that.webSend(agentData)
                                }, 500)
                        }
                }
        }       
        webMessage(e){
                this.callback(e)
        }
        webSend(agentData){
                this.websock.send(agentData)        
        }
        webClose(e){
                this.websock.close()
        }
        webOpen(){}
}

export default Socket

第二步:在vue文件里引入使用

socket的使用

//引入socket
import socket from "@/libs/socket.js"; //根据自己放的路径来引入socket

methods:{
        //socket操作
        initWebsocket(){
                console.log("初始化websocket");
                if(!this.websocket){
                        this.websocket = new socket(
                                //这里填写要请求socket的url字符串
                        )
                        this.websocket.sendSock("", this.websocketonmessage);
                }
        },
        websocketonmessage(e){
                console.log(e.data)
        },
        closeWebSocket(){
                if(this.websocket != null){
                        this.websocket.webClose();
                }
        }
},
mounted(){
        this.initWebsocket()
},
beforeDestroy(){
        this.closeWebSocket()
}

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Chen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值