Connect and disconnect sharing foundations.

master
Ben Burlingham 5 years ago
parent 080643d8ac
commit 32c8a045c0
  1. 16
      game.js
  2. 34
      ricochet.html
  3. 47
      server.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;

@ -65,8 +65,14 @@
<body>
<div class="controls-container">
<div>
<span>Room ID</span>
<input type="text" id='game-id'>
<button type='button'>&gt;</button>
</div>
<div class="rounds">
<button type='button' id='start'>Start New Round</button>
<button type='button' id='game-start'>Start New Round</button>
<div class="timer">0:42</div>
</div>
@ -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!')
})

@ -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")
Loading…
Cancel
Save