-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws_server.js
39 lines (38 loc) · 1.22 KB
/
ws_server.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
let WSServer=require("ws").Server;
let ws=new WSServer({port:5001});
let connections = [];
function broadcast(message) {
connections.forEach((con, i)=> {
con.send(message);
});
};
ws.on("connection",(ws)=>{
connections.push(ws);
ws.on("close",()=>{
connections=connections.filter((conn,i)=>{
return (conn===ws)?false:true;
});
});
ws.on("message",(message)=>{
let objtdn=JSON.parse(message);
switch(objtdn.command){
case "test":
let objkun={
"command":"selectimg",
"data":[
"https://via.placeholder.com/300x300",
"https://via.placeholder.com/300x300",
"https://via.placeholder.com/300x300",
"https://via.placeholder.com/300x300",
"https://via.placeholder.com/300x300"
]
}
broadcast(JSON.stringify(objkun));
break;
case "selectedimg":
console.log("selected ! %d",objtdn.indexid);
break;
}
//console.log("recieved: %s",JSON.stringify(objtdn))
})
});