parent
080643d8ac
commit
32c8a045c0
3 changed files with 85 additions and 12 deletions
@ -0,0 +1,16 @@ |
|||||||
|
const uuid = require('node-uuid'); |
||||||
|
|
||||||
|
const Game = function() { |
||||||
|
this.id = uuid.v4(); |
||||||
|
|
||||||
|
this.walls = [ |
||||||
|
{x: 8, y: 9, n: 1, e: 0, s: 0, w: 1 }, |
||||||
|
{x: 18, y: 9, n: 1, e: 0, s: 0, w: 1 }, |
||||||
|
{x: 4, y: 19, n: 1, e: 0, s: 0, w: 1 } |
||||||
|
]; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = Game; |
@ -1,30 +1,59 @@ |
|||||||
const WebSocket = require('ws'); |
const WebSocket = require('ws'); |
||||||
const uuid = require('node-uuid'); |
const uuid = require('node-uuid'); |
||||||
|
|
||||||
|
const jsDir = `${__dirname}/`; |
||||||
|
const Game = require(`${jsDir}/game.js`); |
||||||
|
|
||||||
const wss = new WebSocket.Server({ port: 8080 }); |
const wss = new WebSocket.Server({ port: 8080 }); |
||||||
|
|
||||||
const DEBUG = true; |
const DEBUG = true; |
||||||
|
|
||||||
|
// Global, for now. Is there a need for an instance? Ben 052220
|
||||||
const Server = { |
const Server = { |
||||||
messageAll: (ws, message) => {
|
games: {}, |
||||||
|
|
||||||
|
messageOthers: (ws, message) => {
|
||||||
|
DEBUG && console.log("Sending to other " + wss.clients.size + " clients.") |
||||||
wss.clients.forEach((client) => { |
wss.clients.forEach((client) => { |
||||||
if (client !== ws && client.readyState === WebSocket.OPEN) { |
if (client !== ws && client.readyState === WebSocket.OPEN) { |
||||||
client.send(ws.id + ' ' + message); |
client.send(JSON.stringify(message)); |
||||||
|
} |
||||||
|
}); |
||||||
|
}, |
||||||
|
|
||||||
|
messageAll: (ws, message) => {
|
||||||
|
DEBUG && console.log("Sending to all " + wss.clients.size + " clients.") |
||||||
|
wss.clients.forEach((client) => { |
||||||
|
if (client.readyState === WebSocket.OPEN) { |
||||||
|
client.send(JSON.stringify(message)); |
||||||
} |
} |
||||||
}); |
}); |
||||||
}, |
}, |
||||||
|
|
||||||
|
onDisconnect: (ws) => {
|
||||||
|
DEBUG && console.log("Closing " + ws.id) |
||||||
|
Server.messageOthers(ws, { head: { type: 'disconnect' }, body: { id: ws.id }}) |
||||||
|
}, |
||||||
|
|
||||||
|
onConnect: (ws, req) => { |
||||||
|
ws.id = uuid.v4(); |
||||||
|
DEBUG && console.log(req.url + ' connected to ' + ws.id); |
||||||
|
|
||||||
|
ws.on('message', Server.onMessage.bind(null, ws)); |
||||||
|
ws.on('close', Server.onDisconnect.bind(null, ws)) |
||||||
|
|
||||||
|
const G = new Game(); |
||||||
|
|
||||||
|
Server.messageAll(ws, { head: { type: 'connect' }, body: { id: ws.id }}); |
||||||
|
Server.messageAll(ws, { head: { type: 'walls' }, body: G.walls}) |
||||||
|
}, |
||||||
|
|
||||||
onMessage: (ws, message) => { |
onMessage: (ws, message) => { |
||||||
DEBUG && console.log('Received: %s', message); |
DEBUG && console.log('Received: %s', message); |
||||||
Server.messageAll(ws, message); |
// Server.messageAll(ws, message);
|
||||||
}, |
}, |
||||||
}; |
}; |
||||||
|
|
||||||
wss.on('connection', function connection(ws, req) { |
wss.on('connection', Server.onConnect); |
||||||
ws.id = uuid.v4(); |
|
||||||
DEBUG && console.log(req.url + ' connected to ' + ws.id); |
|
||||||
|
|
||||||
ws.on('message', Server.onMessage.bind(null, ws)); |
|
||||||
}); |
|
||||||
|
|
||||||
console.log("Websocket server listening on :8080") |
console.log("Websocket server listening on :8080") |
Loading…
Reference in new issue