vue里面websocket

一.设置websocket标识

websock:null

二.method里面

//初始化websocket
      initWebSocket(){
        var _self = this;
        var currentTime = new Date();
        _self.websock = new WebSocket("ws://127.0.0.1:8088/chatsocket?openid="+_self.openid+"&date="+currentTime);
        _self.websock.onopen = this.websocketonopen;
        _self.websock.onerror = this.websocketonerror;
        _self.websock.onmessage = this.websocketonmessage;
        _self.websock.onclose = this.websocketclose;
      },
      websocketonopen(){
        var _self = this;
        var param = {};
        param["openid"] = _self.openid;
        param["text"] = "Hi";
        param["uuid"] = _self.uuid;
        param = JSON.stringify(param);
        this.websocketsend(param);
      },
      websocketsend(agentData){//数据发送
        this.websock.send(agentData);
      },
      websocketonerror(e){
        var _self = this;
        _self.websock = null;
      },
      websocketonmessage(e){
        var msg = JSON.parse(e.data);
        var obj = {};
        obj['id'] = 1;
        obj['message'] = msg.Text;
        //存我发出的消息
        this.$store.commit('addDialog',obj);
      },
      //销毁websocket
      websocketclose(){
        var _self = this;
        _self.websock = null;
      },

三.mounted调用

mounted : function() {
    _self.initWebSocket();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值