diff --git a/game.js b/game.js new file mode 100644 index 0000000..a506f8c --- /dev/null +++ b/game.js @@ -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; \ No newline at end of file diff --git a/ricochet.html b/ricochet.html index 639d0bf..08dc3e1 100644 --- a/ricochet.html +++ b/ricochet.html @@ -65,8 +65,14 @@
+
+ Room ID + + +
+
- +
0:42
@@ -124,6 +130,27 @@ // TODO move websocket server to /core // TODO dynamic socket server resolution // TODO namespace server to /ricochet + // TODO [soap, xmpp] + // TODO a message must have a head and a body + // TODO your favorite games + + const Cookie = { + getCookie: function(name) { + var v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)'); + return v ? decodeURI(v[2]) : null; + }, + + setCookie: function(name, value, days) { + var d = new Date; + d.setTime(d.getTime() + 24*60*60*1000*days); + document.cookie = name + "=" + encodeURI(value) + ";path=/;expires=" + d.toGMTString(); + }, + + deleteCookie: function(name) { + Util.setCookie(name, '', -1); + } + }; + // https://github.com/websockets/ws // const WebSocket = require('ws'); @@ -141,7 +168,8 @@ // BROWSER var connection = new WebSocket('ws://localhost:8080/ricochet', ['soap', 'xmpp']); // When the connection is open, send some data to the server -connection.onopen = function () { +connection.onopen = function (aaa) { + console.log(aaa) connection.send('Ping'); // Send the message 'Ping' to the server }; @@ -155,7 +183,7 @@ connection.onmessage = function (e) { console.log('Server: ' + e.data); }; -document.getElementById('start').addEventListener('click', () => { +document.getElementById('game-start').addEventListener('click', () => { connection.send('says hello!') }) diff --git a/server.js b/server.js index 6b8e02e..b13e201 100644 --- a/server.js +++ b/server.js @@ -1,30 +1,59 @@ const WebSocket = require('ws'); const uuid = require('node-uuid'); +const jsDir = `${__dirname}/`; +const Game = require(`${jsDir}/game.js`); + const wss = new WebSocket.Server({ port: 8080 }); const DEBUG = true; +// Global, for now. Is there a need for an instance? Ben 052220 const Server = { - messageAll: (ws, message) => { + games: {}, + + messageOthers: (ws, message) => { + DEBUG && console.log("Sending to other " + wss.clients.size + " clients.") wss.clients.forEach((client) => { 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) => { DEBUG && console.log('Received: %s', message); - Server.messageAll(ws, message); + // Server.messageAll(ws, message); }, }; -wss.on('connection', function connection(ws, req) { - ws.id = uuid.v4(); - DEBUG && console.log(req.url + ' connected to ' + ws.id); - - ws.on('message', Server.onMessage.bind(null, ws)); -}); +wss.on('connection', Server.onConnect); console.log("Websocket server listening on :8080") \ No newline at end of file