-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathsocket.ts
More file actions
234 lines (210 loc) · 5.94 KB
/
socket.ts
File metadata and controls
234 lines (210 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { io, Socket } from 'socket.io-client';
import _isNil from 'lodash/isNil';
import { getServiceUrl } from '../manager/service';
import { isDevelopment } from '../utils/environment';
import { showErrorToasts, showGlobalLoading, showToasts } from '../manager/ui';
import { t } from '../i18n';
import { sharedEvent } from '../event';
import msgpackParser from 'socket.io-msgpack-parser';
class SocketEventError extends Error {
name = 'SocketEventError';
}
type SocketEventRespones<T = unknown> =
| {
result: true;
data: T;
}
| {
result: false;
message: string;
};
/**
* 封装后的 Socket
*/
export class AppSocket {
private listener: [string, (data: unknown) => void][] = [];
constructor(private socket: Socket) {
socket.onAny((eventName: string, data: unknown) => {
const matched = this.listener.filter(([ev]) => ev === eventName); // 匹配到的监听器列表
if (matched.length === 0) {
// 没有匹配到任何处理函数
console.warn(`[Socket IO] Unhandler event: ${eventName}`, data);
return;
}
matched.forEach(([, cb]) => {
cb(data);
});
});
}
get connected(): boolean {
return this.socket.connected;
}
async request<T = unknown>(
eventName: string,
eventData: unknown = {}
): Promise<T> {
return new Promise((resolve, reject) => {
this.socket.emit(eventName, eventData, (resp: SocketEventRespones<T>) => {
if (resp.result === true) {
resolve(resp.data);
} else if (resp.result === false) {
const error = new SocketEventError(
`[${eventName}]: ${resp.message}: \ndata: ${JSON.stringify(
eventData
)}`
);
console.error(error);
reject(error);
}
});
});
}
/**
* 监听远程通知
*/
listen<T>(eventName: string, callback: (data: T) => void) {
this.listener.push([`notify:${eventName}`, callback as any]);
}
/**
* 移除监听函数
*/
removeListener(eventName: string, callback: (data: any) => void) {
const index = this.listener.findIndex(
(item) => item[0] === `notify:${eventName}` && item[1] === callback
);
if (index >= 0) {
this.listener.splice(index, 1);
}
}
/**
* 模拟重连
* NOTICE: 仅用于开发环境
*/
mockReconnect() {
this.socket.disconnect();
showToasts('reconnect after 5s');
setTimeout(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.socket.io.skipReconnect = false;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.socket.io.reconnect();
}, 5 * 1000);
}
/**
* 断线重连后触发
*/
onReconnect(cb: () => void) {
this.socket.io.on('reconnect', cb);
}
/**
* 断开连接
*/
disconnect() {
this.socket.disconnect();
}
/**
* 初始Socket状态管理提示
*/
private closeFn: unknown = null; // 全局loading关闭函数
setupSocketStatusTip() {
const socket = this.socket;
const showConnecting = () => {
if (this.closeFn) {
return;
}
this.closeFn = showGlobalLoading(t('正在重新链接'));
};
const closeConnecting = () => {
if (this.closeFn && typeof this.closeFn === 'function') {
this.closeFn();
this.closeFn = null;
}
};
// 网络状态管理
socket.on('connect', () => {
console.log('连接成功');
closeConnecting();
sharedEvent.emit('updateNetworkStatus', 'connected');
});
socket.on('connecting', (data) => {
console.log('正在连接');
showConnecting();
sharedEvent.emit('updateNetworkStatus', 'reconnecting');
});
socket.on('disconnect', (data) => {
console.log('与服务器的链接已断开');
showErrorToasts(t('与服务器的链接已断开'));
closeConnecting();
sharedEvent.emit('updateNetworkStatus', 'disconnected');
});
socket.on('connect_error', (data) => {
console.log('连接失败');
showErrorToasts(t('连接失败'));
closeConnecting();
sharedEvent.emit('updateNetworkStatus', 'disconnected');
});
socket.io.on('reconnect', (data) => {
console.log('重连成功');
closeConnecting();
sharedEvent.emit('updateNetworkStatus', 'connected');
});
socket.io.on('reconnect_attempt', (data) => {
console.log('重连中...');
showConnecting();
sharedEvent.emit('updateNetworkStatus', 'reconnecting');
});
socket.io.on('reconnect_error', (error) => {
console.error('重连尝试失败...', error);
showConnecting();
sharedEvent.emit('updateNetworkStatus', 'reconnecting');
});
socket.io.on('reconnect_failed', () => {
console.error('重连失败...');
showConnecting();
sharedEvent.emit('updateNetworkStatus', 'disconnected');
});
socket.io.on('error', (error) => {
console.error('网络出现异常', error);
showErrorToasts(t('网络出现异常'));
closeConnecting();
sharedEvent.emit('updateNetworkStatus', 'disconnected');
});
}
}
let _socket: Socket;
/**
* 创建Socket连接
* 如果已经有Socket连接则关闭上一个
* @param token Token
*/
export function createSocket(token: string): Promise<AppSocket> {
if (!_isNil(_socket)) {
_socket.close();
}
return new Promise((resolve, reject) => {
_socket = io(getServiceUrl(), {
transports: ['websocket'],
auth: {
token,
},
forceNew: true,
parser: msgpackParser,
});
_socket.once('connect', () => {
// 连接成功
const appSocket = new AppSocket(_socket);
appSocket.setupSocketStatusTip();
resolve(appSocket);
});
_socket.once('error', () => {
reject();
});
if (isDevelopment) {
_socket.onAny((...args) => {
console.debug('Receive Notify:', args);
});
}
});
}