-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserver.js
More file actions
99 lines (77 loc) · 2.77 KB
/
Copy pathserver.js
File metadata and controls
99 lines (77 loc) · 2.77 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
const express = require('express');
const SocketServer = require('ws').Server;
const path = require('path');
// list of currently connected clients
var drawing_clients = {};
var authoring_clients = {};
var keys = ["jen","fish","ben","joel","radomir","golan","sarah","cara","james","jesse","demo1","demo2"];
const PORT = process.env.PORT || 3000;
const INDEX = path.join(__dirname, 'index.html');
const server = express()
.use((req, res) => res.sendFile(INDEX))
.listen(PORT, () => console.log(`Listening on ${ PORT }`));
const wss = new SocketServer({
server
});
wss.on('connection', (ws) => {
console.log('Client connected', ws.protocol);
var userkey = ws.protocol.split("_")[1];
var connection = ws;
if(keys.find(function(e){return e == userkey;})){
connection.send("key recognized");
var protocol = ws.protocol;
var clientName = ws.protocol.split("_")[0];
if (clientName == 'drawing') {
drawing_clients[userkey] = connection;
if (authoring_clients[userkey]) {
authoring_clients[userkey].send("drawing client connected");
}
} else if (clientName == 'authoring') {
authoring_clients[userkey] = connection;
if (drawing_clients[userkey]) {
console.log("sending authoring connected message");
drawing_clients[userkey].send("authoring_client_connected");
}
}
ws.on('message', function incoming(message) {
console.log('message', clientName, userkey, message);
var json_data = JSON.parse(message);
if (json_data.type == "synchronize" || json_data.type == "behavior_data" || json_data.type == "authoring_response" || json_data.type == "storage_data" ) {
if (authoring_clients[userkey]) {
authoring_clients[userkey].send(JSON.stringify(json_data));
}
}
if (json_data.type == "brush_init") {
ws.send("init_data_received");
} else {
ws.send("message received");
}
if (json_data.type == "data_request" || json_data.type == "synchronize_request" || json_data.type == "authoring_request" || json_data.type == "storage_request") {
if(json_data.requester == "authoring" && authoring_clients[userkey] && drawing_clients[userkey]){
console.log("requesting authoring response from drawing client");
drawing_clients[userkey].send(JSON.stringify(json_data));
}
}
});
ws.on('close', function close() {
console.log(clientName +' '+userkey+ 'client disconnected');
if (clientName == "authoring") {
delete authoring_clients[userkey];
if(drawing_clients[userkey]){
drawing_clients[userkey].send("authoring client disconnected");
}
}
if (clientName == "drawing") {
delete drawing_clients[userkey];
if(authoring_clients[userkey]){
authoring_clients[userkey].send("drawing client disconnected");
}
}
});
}
else{
console.log("key not recognized");
connection.send("key not recognized");
}
});