首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue-WebSocket的使用

Vue-WebSocket的使用

原创
作者头像
〆 千寻、
修改2020-04-20 18:12:43
1.7K0
修改2020-04-20 18:12:43
举报
文章被收录于专栏:技术分享文章技术分享文章

如果不是很明白的话,可以看下讲解 WebSocket

data(){
    return{
        websock:null
    }
}
created() {
            this.initWebSocket()//初始化weosocket
        },
methods:{
         /***初始化weosocket***/
            initWebSocket(){
                const wsuri = `ws://192.168.0.0:8083/xxxxxx/websocket`//后端提供地址
                this.websock = new WebSocket(wsuri);
                this.websock.onmessage = this.websocketonmessage;
                this.websock.onopen = this.websocketonopen;
                this.websock.onerror = this.websocketonerror;
                this.websock.onclose = this.websocketclose;
            },
            /***连接建立执行send方法发送数据***/
            websocketonopen(){
                // this.websocketsend()
            },
            /***连接失败重新连接***/
            websocketonerror(){
                this.initWebSocket()
            },
            /***接收数据***/
            websocketonmessage(result){
                console.log('接收到新消息=',result)
            },
            /***数据发送***/
            websocketsend(data){//数据发送
                console.log(data)
                // this.websock.send(data)
            },
            /***断开连接***/
            websocketclose(e){ 
                console.log('断开连接', e)
            }
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
即时通信 IM
即时通信 IM(Instant Messaging)基于腾讯二十余年的 IM 技术积累,支持Android、iOS、Mac、Windows、Web、H5、小程序平台且跨终端互通,低代码 UI 组件助您30分钟集成单聊、群聊、关系链、消息漫游、群组管理、资料管理、直播弹幕和内容审核等能力。适用于直播互动、电商带货、客服咨询、社交沟通、在线课程、企业办公、互动游戏、医疗健康等场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档