-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
68 lines (62 loc) · 2.02 KB
/
app.js
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
const WebSocket = require("ws");
const ClientManager = require("./ClientManager");
const TextMessage = require("./message/TextMessage");
const clientManager = new ClientManager();
global.clientManager = clientManager;
// exports.clientManager = global.clientManager;
const webSocketServer = new WebSocket.WebSocketServer({
port: 8080,
perMessageDeflate: false,
// verifyClient: (info, done) => {
// const authToken = info.req.headers.authorization;
// authenticateUser
// },
});
webSocketServer.on("listening", () => {
console.log(
"Server is listening: ws://localhost:" + webSocketServer.address().port
);
});
webSocketServer.on("connection", (ws, request) => {
// create a new client object and add it to the client manager
// id can be used to identify the client
// you should create a new user via http, and assign a unique id to it
// TODO:(@branlice): the constructor WebSocketServer provides a cache of all clients,
// stored in the ws.clients member variable,
// to investigate whether querying a single client is supported
const clientId = new Date().getTime().toString();
clientManager.register(clientId, ws);
ws.on("message", (message) => {
const messageObject = JSON.parse(message);
try {
if (messageObject?.type === "text") {
try {
new TextMessage(
clientId,
messageObject.to,
messageObject.text,
{}
).singleton();
} catch (error) {
console.log("Error: ", error);
}
// const toClient = clientManager.getClient(messageObject.to);
// if (toClient) {
// toClient.send(messageObject.text);
// }
}
} catch (error) {
console.log(error);
}
ws.send(messageObject.text);
});
ws.on("close", () => {
console.log("Client disconnected");
clientManager.unregister(clientId);
});
ws.on("error", (error) => {
console.log(error);
clientManager.unregister(clientId);
});
ws.send(clientId);
});