Websocket communication foundation.

master
Ben Burlingham 5 years ago
parent f2eca30432
commit 080643d8ac
  1. 5
      package-lock.json
  2. 3
      package.json
  3. 8
      ricochet.html
  4. 31
      server.js

5
package-lock.json generated

@ -4,6 +4,11 @@
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"node-uuid": {
"version": "1.4.8",
"resolved": "https://registry.npmjs.org/node-uuid/-/node-uuid-1.4.8.tgz",
"integrity": "sha1-sEDrCSOWivq/jTL7HxfxFn/auQc="
},
"ws": {
"version": "7.3.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-7.3.0.tgz",

@ -4,11 +4,12 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "node server.js"
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"node-uuid": "^1.4.8",
"ws": "^7.3.0"
}
}

@ -66,7 +66,7 @@
<div class="controls-container">
<div class="rounds">
<button type='button'>Start New Round</button>
<button type='button' id='start'>Start New Round</button>
<div class="timer">0:42</div>
</div>
@ -139,7 +139,7 @@
// });
// BROWSER
var connection = new WebSocket('ws://localhost:8080/', ['soap', 'xmpp']);
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.send('Ping'); // Send the message 'Ping' to the server
@ -155,6 +155,10 @@ connection.onmessage = function (e) {
console.log('Server: ' + e.data);
};
document.getElementById('start').addEventListener('click', () => {
connection.send('says hello!')
})
const board = document.getElementById('board');
const squaresPerSide = 20;
const squareSize = 40;

@ -1,11 +1,30 @@
const WebSocket = require('ws');
const uuid = require('node-uuid');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
const DEBUG = true;
ws.send('something');
});
const Server = {
messageAll: (ws, message) => {
wss.clients.forEach((client) => {
if (client !== ws && client.readyState === WebSocket.OPEN) {
client.send(ws.id + ' ' + message);
}
});
},
onMessage: (ws, message) => {
DEBUG && console.log('Received: %s', 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));
});
console.log("Websocket server listening on :8080")
Loading…
Cancel
Save