forked from r03ert0/microdraw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicrodrawServer.js
More file actions
46 lines (36 loc) · 933 Bytes
/
microdrawServer.js
File metadata and controls
46 lines (36 loc) · 933 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
37
38
39
40
41
42
43
44
45
46
/*
Microdraw Server
Roberto Toro, 24 February 2016
*/
var debug=1;
var WebSocketServer=require("ws").Server; //https://github.com/websockets/ws
console.log("microdrawServer.js");
initSocketConnection();
function initSocketConnection() {
// WS connection
var host = "ws://localhost:8080";
try {
websocket = new WebSocketServer({port:8080});
websocket.on("connection",function(s) {
s.on('message',function(msg) {
var data=JSON.parse(msg);
// integrate paint messages
switch(data.type) {
case "setBounds":
// broadcast
for(var i in websocket.clients) {
if(websocket.clients[i]==s)
continue;
websocket.clients[i].send(JSON.stringify(data));
}
break;
}
});
s.on('close',function(msg) {
console.log(new Date(),"[connection: close]");
});
});
} catch (ex) {
console.log(new Date(),"ERROR: Unable to create a server",ex);
}
}