因业务需要,做大屏页面某一处需要不断推送消息,采用websocket技术;
1. 新建socket.ts
在utils文件夹下新建socket.ts;
export default class Socket {
// cb: 存储回调函数
constructor(url: string, cb: (data: any) => void) {
this.url = url;
this.cb = cb;
this.connect();
}
private readonly url: string = '';
private cb?: (data: any) => void;
// 和服务端连接的socket对象
private ws?: WebSocket;
private remainPayload: any[] = [];
public connect() {
return new Promise<void>(resolve => {
const ws = new WebSocket(this.url);
ws.