-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
36 lines (26 loc) · 837 Bytes
/
index.js
File metadata and controls
36 lines (26 loc) · 837 Bytes
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
const ws = require("ws");
const Logger = require("./logger")
const Reader = require("./reader")
const server = new ws.Server({ port: 3000 })
new Logger().setup();
const clients = {};
server.on('connection', (socket, req) => {
this.clientId = generateClientId();
new Logger().success(`Connect cliend ID:${this.clientId}`, req.socket.remoteAddress);
clients[this.clientId] = socket;
socket.send(JSON.stringify({
type: 0,
status: 200,
id: this.clientId,
}))
socket.on('message', (data) => {
console.log(new Reader(data).decode());
//this.data = JSON.parse(new Reader(data).decode());
})
socket.on('close', (data, reason) => {
delete clients[this.clientId];
})
})
function generateClientId() {
return Math.random().toString(36).substr(2, 9);
}