handleMessage = () => {
const url = '////'; //某url
const token = getCookie('xnToken');//向后端发请求得登陆用户信息
console.log(token);
// websocket 函数
this.addWebsocketHandle = () => {
var ws = new WebSocket(url);
console.log(ws);
ws.onopen = () => {
console.log('与服务器建立连接。。。。。。');
const content = JSON.stringify({ token });
ws.send(content);
};
// 接受服务端数据时触发事件
ws.onmessage = (event) => {console.log(event.data);
};
ws.onclose = () => {
console.log('服务器断开连接。。。。。。。。。。');
return this.addWebsocketHandle(); // 这里重新连接websocket
};
this.addWebsocketHandle();
}