class MyWs { constructor() { this.ws = null; // this.ws = new WebSocket(); } linkWs(url) { if (this.ws && this.ws.readyState == 1) return this.ws = new WebSocket(url) } sendMessage(data) { console.log(this); if (this.ws && this.ws.readyState == 1) { let obj = { cmd: 1, data: { name: '123', } } this.send(obj) obj = { cmd: 4, data: { msg: data, } } this.send(obj) // obj = { // cmd: 6, // data: { // msg: data, // } // } // this.send(obj,this.ws) } } send(obj) { console.log(2323); this.ws.send(JSON.stringify(obj)) } close(){ if (this.ws && this.ws.readyState == 1) { this.ws.close() } } } export default new MyWs();