-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cloud_Server.js
56 lines (54 loc) · 1.8 KB
/
Cloud_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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const WebSocket = require('ws');
const fs = require('fs');
const PORT = '1206'
var temp_json = ''
var people = 0
var real_json = [{ 'name': "Server" }]
const server = new WebSocket.Server({ port: PORT })
server.on('connection', function (ws) {
people++
console.log(people)
ws.on('message', function (message) {
temp_json = JSON.parse(message.toString())
if (temp_json['name'] != undefined) {
for (var i in real_json) {
if (real_json[i]['name'] == 'Server') {
real_json.splice(i, 1)
real_json.push(temp_json)
}
else if (real_json[i]['name'] == temp_json['name']) {
if (temp_json['val'] == 'disconnect') {
real_json.splice(i, 1)
}
else {
real_json[i]['val'] = temp_json['val']
}
break
}
else if (real_json[i]['name'] != temp_json['name']) {
if (i == real_json.length - 1) {
real_json.push(temp_json)
}
}
}
server.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(real_json));
}
})
}
else if (temp_json['cmd'] == 'GVar'){
server.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(temp_json));
}
})
}
})
ws.on('close', function () {
people--
real_json = [{ 'name': "Server" }]
console.log('-1')
console.log(people)
})
})