This repository was archived by the owner on Nov 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp2.js
205 lines (175 loc) · 6.32 KB
/
app2.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const WebSocket = require('ws');
const wss = new WebSocket.Server({
port: 3000
});
var names_con = [];
var names_dis = [];
var lookup_dis = {};
var lookup_con = {};
wss.on('connection', function connection(ws) {
console.log('Verbindung von Client');
ws.on('message', function incoming(data) {
console.log("Neue Nachricht: " + data);
var obj = JSON.parse(data);
switch (obj.id) {
case "LOGIN":
if (typeof obj.name !== "undefined" && obj.name != "") {
console.log(obj.name + " has logged in");
if (names.includes(obj.name) || obj.name == "temp") {
console.log("name schon vorhanden");
lookup_dis["temp"] = ws;
lookup_dis["temp"].send(JSON.stringify({
id: "LOGIN_ATTEMPT",
confirm: "false"
}));
delete lookup_dis["temp"];
} else {
if (obj.device == "1") {
lookup_con[obj.name] = ws;
names.push(obj.name);
lookup_con[obj.name].send(JSON.stringify({
id: "LOGIN_ATTEMPT",
confirm: "true"
}));
console.log("logged in as controller");
}
if (obj.device == "2") {
lookup_dis[obj.name] = ws;
names.push(obj.name);
lookup_dis[obj.name].send(JSON.stringify({
id: "LOGIN_ATTEMPT",
confirm: "true"
}));
console.log("logged in as display");
}
}
} else {
console.log("name is undefined or empty");
}
break;
case "SEND_TO":
if (typeof obj.to_name !== "undefined" && obj.to_name != "" && typeof lookup[obj.to_name] !== "undefined") {
console.log("send to " + obj.to_name + " this message: " + obj.msg);
lookup[obj.to_name].send(obj.msg);
} else {
console.log("user ist not avilable");
}
break;
case "Ask Controldler":
if (typeof obj.con1 !== "undefined" && obj.con1 != "" && typeof lookup_con[obj.con1] !== "undefined" &&
typeof obj.con2 !== "undefined" && obj.con2 != "" && typeof lookup_con[obj.con2] !== "undefined" && obj.con1 !== obj.con2) {
lookup_con[obj.con1].send(obj.name + " want to connect");
lookup_con[obj.con2].send(obj.name + " want to connect");
}
break;
case "Display":
while (true) {
var name = Math.floor(Math.random() * (9999 - 1000) + 1000);
if (names_dis.includes(String(name)) !== true) {
lookup_dis[String(name)] = ws;
names_dis.push(String(name));
lookup_dis[String(name)].send(JSON.stringify({
id: "LOGIN",
name: String(name)
}));
console.log("New Display : " + name);
break;
}
}
break;
case "CONTROLLER":
if (typeof obj.name !== "undefined" && obj.name != "" && names_dis.includes(obj.name)) {
if (names_con.includes(obj.name + obj.orien)) {
if (obj.orien == "_right") {
if (names_con.includes(obj.name + "_left")) {
lookup_con["temp"] = ws;
lookup_con["temp"].send(JSON.stringify({
id: "CONTROLLER",
orien: "nothing"
}));
delete lookup_con["temp"];
} else {
names_con.push(obj.name + "_left");
lookup_con[(obj.name + "_left")] = ws;
lookup_con[(obj.name + "_left")].send(JSON.stringify({
id: "CONTROLLER",
orien: "_left"
}));
//___________________
lookup_dis[obj.name].send(JSON.stringify({
id: "CONTROLLER CONNECT",
orien: "_left"
}));
console.log(obj.name + "_left");
}
} else {
if (names_con.includes(obj.name + "_right")) {
lookup_con["temp"] = ws;
lookup_con["temp"].send(JSON.stringify({
id: "CONTROLLER",
orien: "nothing"
}));
delete lookup_con["temp"];
} else {
names_con.push(obj.name + "_right");
lookup_con[(obj.name + "_right")] = ws;
lookup_con[(obj.name + "_right")].send(JSON.stringify({
id: "CONTROLLER",
orien: "_right"
}));
//______________________
lookup_dis[obj.name].send(JSON.stringify({
id: "CONTROLLER CONNECT",
orien: "_right"
}));
console.log(obj.name + "_right");
}
}
//Send back (faliure)
} else {
names_con.push(obj.name + obj.orien);
lookup_con[(obj.name + obj.orien)] = ws;
lookup_con[(obj.name + obj.orien)].send(JSON.stringify({
id: "CONTROLLER",
orien: obj.orien
}));
console.log(obj.name + obj.orien);
//___________________
lookup_dis[obj.name].send(JSON.stringify({
id: "CONTROLLER CONNECT",
orien: obj.orien
}));
}
} else {
lookup_con["temp"] = ws;
lookup_con["temp"].send(JSON.stringify({
id: "CONTROLLER",
orien: "nothing"
}));
delete lookup_con["temp"];
}
break;
case "GO":
if (typeof obj.name !== "undefined" && obj.name != "" && names_dis.includes(obj.name) && names_con.includes(obj.name + "_right") && names_con.includes(obj.name + "_right")) {
lookup_con[obj.name + "_left"].send(JSON.stringify({
id: "GO"
}));
lookup_con[obj.name + "_right"].send(JSON.stringify({
id: "GO"
}));
} else {
console.log("Error");
}
break;
case "GAME":
if (typeof obj.name !== "undefined" && obj.name != "" && names_dis.includes(obj.name)) {
lookup_dis[obj.name].send(JSON.stringify({
id: "GAME",
orien: obj.orien,
y: obj.y
}))
}
break;
} //switch
});
});