在vue里用原生的websocket,连接成功但是无法发送

client.js 如下

const ws = {
    wsServer:"ws://xxxxxx:xxx",
    websocket:null,
    init:function () {
        this.websocket = new WebSocket(this.wsServer);
        this.websocket.onopen = function (evt) {
            console.log("连接到websocket服务端");
            this.websocket.send('{"manager_id": "1"}');
        };
        this.websocket.onclose = function (evt) {
            console.log("关闭socket");
        };
        this.websocket.onmessage = function (evt) {
            console.log('接收服务端推送的信息: ' + evt.data);
        };
        this.websocket.onerror = function (evt) {
            console.log('socket连接错误: ' + evt.data);
        };
    },
    submit:function (msg) {

    }
}

export default ws
阅读 8.3k
2 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题