This repository was archived by the owner on May 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
144 lines (132 loc) · 3.62 KB
/
Copy pathindex.js
File metadata and controls
144 lines (132 loc) · 3.62 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
/*
* @Author: lushijie
* @Date: 2017-10-09 14:00:01
* @Last Modified by: lushijie
* @Last Modified time: 2018-04-13 11:34:42
*/
const WebSocketServer = require('ws').Server;
const helper = require('think-helper');
const mockHttp = require('think-mock-http');
const WS_STATUS = {
CONNECTING: 0,
OPEN: 1,
CLOSING: 2,
CLOSED: 3
};
const getDestructResult = (properties, sourceObject) => {
const destObject = {};
const aliasMap = {};
properties = Array.isArray(properties) ? properties : [properties];
properties = properties.map(function(ele) {
if (ele.indexOf(':') > -1) {
aliasMap[ele.split(':')[0]] = ele.split(':')[1];
}
return ele.split(':')[0];
});
Object.keys(sourceObject).forEach(function(ele) {
if (properties.indexOf(ele) > -1) {
if (sourceObject[ele] !== undefined) {
destObject[aliasMap[ele] || ele] = sourceObject[ele];
}
}
});
return destObject;
};
module.exports = class WebSocket {
/**
* constructor
*/
constructor(server, config, app) {
this.server = server;
this.config = config;
this.app = app;
const wssConfig = getDestructResult(['host', 'port', 'backlog', 'verifyClient', 'handleProtocols', 'path', 'noServer', 'clientTracking', 'perMessageDeflate', 'maxPayload'], config);
wssConfig.server = server;
this.wss = new WebSocketServer(wssConfig, config.callback || null);
}
/**
* mock request
* @param {String} url
* @param {Object} data
* @param {Object} socket
*/
mockRequst(url, data, socket, request, open, wss) {
if (url[0] !== '/') url = `/${url}`;
const args = {url, websocketData: data, websocket: socket, wss, method: 'WEBSOCKET'};
if (open) {
args.req = request;
}
return mockHttp(args, this.app);
}
/**
* register socket
*/
registerSocket(wss, messages = {}) {
const eventListener = getDestructResult(['onConnection', 'onError', 'onHeaders', 'onListening'], this.config);
wss.on('connection', (socket, request) => {
if (messages.open) {
this.mockRequst(messages.open, undefined, socket, request, true, wss);
}
if (messages.close) {
socket.on('close', () => {
this.mockRequst(messages.close, undefined, socket, undefined, false, wss);
});
}
for (const key in messages) {
if (key === 'open' || key === 'close') continue;
socket.on('message', data => {
data = JSON.parse(data);
if (key === data.event) {
this.mockRequst(messages[key], data, socket, undefined, false, wss);
}
});
}
});
wss.on('listening', eventListener.onListening || (() => {}));
wss.on('headers', eventListener.onHeaders || (() => {}));
wss.on('error', eventListener.onError || (() => {}));
}
/**
* emit an event
* @param {String} event
* @param {Mixed} data
* @param {Object} socket
*/
emit(event, data, socket) {
if (socket.readyState === WS_STATUS.OPEN) {
socket.send(JSON.stringify({
event,
data
}));
};
}
/**
* broadcast event
* @param {String} event
* @param {Mixed} data
* @param {Object} socket
*/
broadcast(event, data, socket) {
this.wss.clients.forEach((client) => {
if (client.readyState === WS_STATUS.OPEN) {
client.send(JSON.stringify({
event,
data
}));
}
});
}
/**
* run
*/
run() {
let messages = this.config.messages || {};
if (!helper.isArray(messages)) {
messages = [messages];
}
messages.forEach(item => {
const wss = this.wss;
this.registerSocket(wss, item);
});
}
};