forked from swwind/ctanet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
33 lines (30 loc) · 1.14 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
const HttpServer = require('http-server')
var WebSocketServer = require("websocketserver")
var localhost = "127.0.0.1"
var server = new WebSocketServer("none", 18465)
var connection_list = {}
server.on("connection", function(id) {
connection_list[id] = {px: -100, py: -100}
console.log('Id ' + id + ' joined the game')
var s = Object.assign(connection_list, {self: id});
server.sendMessage('one', 'upd|' + JSON.stringify(s), id)
server.sendMessage('all', 'pjn|' + id)
})
server.on("message", function(data, id) {
var mes = server.unmaskMessage(data)
var str = server.convertToString(mes.message)
var [px, py] = str.split(',').map(x => parseInt(x))
connection_list[id].px = px
connection_list[id].py = py
server.sendMessage('all', 'pch|' + id + ',' + px + ',' + py)
})
server.on("closedconnection", function(id) {
console.log("Connection " + id + " has left the server")
delete connection_list[id];
server.sendMessage('all', 'pft|' + id)
})
console.log('websocket server started at ' + localhost + ':' + 18465)
const port = 80
var hs = HttpServer.createServer()
hs.listen(port)
console.log('http server started at ' + localhost + ':' + port)