-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
217 lines (194 loc) · 5.79 KB
/
app.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
206
207
208
209
210
211
212
213
214
215
216
217
/*eslint-env node*/
//------------------------------------------------------------------------------
// node.js WebServer + WebSocket Application
//------------------------------------------------------------------------------
// This application uses express as its web server
// for more info, see: http://expressjs.com
let express = require('express')
// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
let cfenv = require('cfenv')
let http = require('http')
let ws = require('ws')
// get the app environment from Cloud Foundry
let appEnv = cfenv.getAppEnv()
// connected clients
let clients = {}
// player info
let players = []
// track active IP Addresses
let active = {}
//
let balls = []
let ballId = 0
// create a new express server
let app = express()
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'))
// Sockets
let httpServer = http.createServer()
let wsServer = new ws.Server({
server: httpServer
})
// WebSocketServer
wsServer.on('connection', function connection(client) {
let id = client.upgradeReq.headers['sec-websocket-key']
clients[id] = client
// Client sent message
client.on('message', function incoming(message, flags) {
handleMessage(wsServer, message, id, client)
})
// Client terminated connection
client.on('close', function incoming(code, message) {
handleClose(wsServer, id)
})
// New client gets an ID from the server
let message = JSON.stringify({
id: 'ID',
from: 'SERVER',
data: {
id: id,
seq: wsServer.clients.length - 1
}
})
client.send(message)
console.log('%s SND <%s>', new Date().getTime(), message)
})
// Start
httpServer.on('request', app)
httpServer.listen(appEnv.port, function () {
console.log("httpServer starting on " + appEnv.url)
})
function broadcast(server, message) {
console.log('%s SND <%s> (%d clients)', new Date().getTime(), message, server.clients.length)
server.clients.forEach(function each(client) {
try {
client.send(message)
} catch (e) {
console.log(e)
}
})
}
function forward(message, active) {
console.log('%s FWD <%s> (%d clients)', new Date().getTime(), message, active.length)
active.forEach(function each(id) {
try {
clients[id].send(message)
} catch (e) {
console.log(e)
}
})
}
function handleMessage(server, message, id, client) {
let ip = client.upgradeReq.connection.remoteAddress
console.log('%s REC <%s>', new Date().getTime(), message)
let msg = JSON.parse(message)
if (!active[ip]) {
active[ip] = true
switch (msg.id) {
case 'JOIN':
players.push({
id: id, name: msg.data.name, screen: msg.data.screen
})
broadcast(server,
JSON.stringify({
id: 'PLAYERS',
from: 'SERVER',
data: {players: players}
}))
break
case 'UPDATE':
let idx = players.findIndex(v => v.id === id)
players[idx] = msg.data.player
broadcast(server,
JSON.stringify({
id: 'PLAYERS',
from: 'SERVER',
data: {players: players}
}))
break
case 'ADD':
if (msg.data.attr.id === undefined) {
msg.data.attr.id = ballId++
balls.push({attr: msg.data.attr, id: id, act: id})
forward(JSON.stringify({
id: 'PUSH',
from: 'SERVER',
data: {attr: msg.data.attr}
}), [id])
}
break
case 'PUSH':
if (players.find(p => p.id === balls[msg.data.attr.id].id)) {
// only forward balls of active players
let to = players[(players.length + players.findIndex(p => p.id === id) + (msg.data.attr.dx > 0 ? 1 : -1)) % players.length].id
balls[msg.data.attr.id].attr = msg.data.attr
balls[msg.data.attr.id].act = to
forward(message, [to])
}
break
case 'CHAT':
broadcast(server, message)
break
default:
console.log('%s ERR <%s>', new Date().getTime(), message)
}
active[ip] = false
} else {
client.send(JSON.stringify({
id: 'FLOOD',
from: 'SERVER',
data: {}
}))
}
}
function handleClose(server, id) {
// console.log(code, message)
delete clients[id]
const idx = players.findIndex(p => p.id === id)
players = players.filter(p => p.id !== id)
// Broadcast: Client has left
broadcast(server,
JSON.stringify({
id: 'EXIT',
from: 'SERVER',
data: {id: id, players: players}
}))
if (players.length > 0) {
let to = players[(players.length + idx - 1) % players.length].id
balls.forEach(b => {
if (b.act === id && b.id !== id) {
b.act = to
forward(JSON.stringify({
id: 'PUSH',
from: 'SERVER',
data: {attr: b.attr}
}), [to])
}
})
} else {
// reset balls info
balls = []
ballId = 0
}
}
function guid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
let r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8)
return v.toString(16)
})
}
let lut = []
for (let i = 0; i < 256; i++) {
lut[i] = (i < 16 ? '0' : '') + (i).toString(16)
}
function guid7() {
let d0 = Math.random() * 0xffffffff | 0
let d1 = Math.random() * 0xffffffff | 0
let d2 = Math.random() * 0xffffffff | 0
let d3 = Math.random() * 0xffffffff | 0
return lut[d0 & 0xff] + lut[d0 >> 8 & 0xff] + lut[d0 >> 16 & 0xff] + lut[d0 >> 24 & 0xff] + '-' +
lut[d1 & 0xff] + lut[d1 >> 8 & 0xff] + '-' + lut[d1 >> 16 & 0x0f | 0x40] + lut[d1 >> 24 & 0xff] + '-' +
lut[d2 & 0x3f | 0x80] + lut[d2 >> 8 & 0xff] + '-' + lut[d2 >> 16 & 0xff] + lut[d2 >> 24 & 0xff] +
lut[d3 & 0xff] + lut[d3 >> 8 & 0xff] + lut[d3 >> 16 & 0xff] + lut[d3 >> 24 & 0xff]
}