-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample-websocket-session.js
53 lines (46 loc) · 1.52 KB
/
example-websocket-session.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
// --------------------------------------------------------------------------
// -- example-websocket-session.js
// -- initial author: Renick Bell ([email protected])
// -- initial creation date: Wed Jun 28 10:08:48 AM CST 2023
// -- contributors: Yiler Huang ([email protected]); Steve Wang ([email protected])
// -- license: GPL 3.0
// --------------------------------------------------------------------------
//const wss = new WebSocketServer.Server({ port: findAvailablePorts(8080, 8090)});
const wss = new WebSocketServer.Server({ port: 8080});
let clients = [];
let newResponseTimes = []
let commands = [
{
action: "hi",
func: function () {
console.log("hi");
},
},{
action: "recordResponseTime",
func: function (info){
newResponseTimes.push(info.date)
}
}
];
// Creating connection using websocket
wss.on("connection", (ws) => {
console.log("new client connected");
addNewClients()
ws.onmessage = (e) => {
console.log('data received')
let data = JSON.parse(e.data)
if (data != undefined) {
//console.log("command received",e.target, e.data);
receiveCommands(data, e.target);
}
};
// handling what to do when clients disconnects from server
ws.on("close", () => {
removeClient(ws)
console.log("the client has connected");
});
// handling client connection error
ws.onerror = function () {
console.log("Some Error occurred");
};
});