lemon橪 2023-01-12 18:12:40 +08:00
commit 7c71375b6c
2 changed files with 840 additions and 844 deletions

File diff suppressed because it is too large Load Diff

View File

@ -6,7 +6,7 @@ export default class SocketService {
* 单例 * 单例
*/ */
static instance = null; static instance = null;
static get Instance() { static get Instance () {
if (!this.instance) { if (!this.instance) {
this.instance = new SocketService(); this.instance = new SocketService();
} }
@ -29,12 +29,12 @@ export default class SocketService {
connectRetryCount = 0; connectRetryCount = 0;
// 定义连接服务器的方法 // 定义连接服务器的方法
connect() { connect () {
// 连接服务器 // 连接服务器
if (!window.WebSocket) { if (!window.WebSocket) {
return console.log("您的浏览器不支持WebSocket"); return console.log("您的浏览器不支持WebSocket");
} }
this.ws = new WebSocket(config.baseWsUrl+'/'+storage.getAccessToken()); this.ws = new WebSocket(config.baseWsUrl + '/' + storage.getAccessToken());
// 连接成功的事件 // 连接成功的事件
this.ws.onopen = () => { this.ws.onopen = () => {
console.log("连接服务端成功"); console.log("连接服务端成功");
@ -54,24 +54,23 @@ export default class SocketService {
}; };
// 得到服务端发送过来的数据 // 得到服务端发送过来的数据
this.ws.onmessage = (msg) => { this.ws.onmessage = (msg) => {
// console.log(msg.data)
this.registerCallBack(msg.data); this.registerCallBack(msg.data);
}; };
} }
// 回调函数的注册 // 回调函数的注册
registerCallBack(callBack) { registerCallBack (callBack) {
// console.log("回调函数的注册", callBack); // console.log("回调函数的注册", callBack);
this.callBackMapping = callBack; this.callBackMapping = callBack;
} }
// 取消某一个回调函数 // 取消某一个回调函数
unRegisterCallBack(callBack) { unRegisterCallBack (callBack) {
console.log("取消某一个回调函数", callBack); console.log("取消某一个回调函数", callBack);
this.callBackMapping = null; this.callBackMapping = null;
} }
// 发送数据的方法 // 发送数据的方法
send(data) { send (data) {
// 判断此时此刻有没有连接成功 // 判断此时此刻有没有连接成功
if (this.connected) { if (this.connected) {
this.sendRetryCount = 0; this.sendRetryCount = 0;