Websocket communication foundation.

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

5
package-lock.json generated

@ -4,6 +4,11 @@
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "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": { "ws": {
"version": "7.3.0", "version": "7.3.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-7.3.0.tgz", "resolved": "https://registry.npmjs.org/ws/-/ws-7.3.0.tgz",

@ -9,6 +9,7 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"node-uuid": "^1.4.8",
"ws": "^7.3.0" "ws": "^7.3.0"
} }
} }

@ -66,7 +66,7 @@
<div class="controls-container"> <div class="controls-container">
<div class="rounds"> <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 class="timer">0:42</div>
</div> </div>
@ -139,7 +139,7 @@
// }); // });
// BROWSER // 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 // When the connection is open, send some data to the server
connection.onopen = function () { connection.onopen = function () {
connection.send('Ping'); // Send the message 'Ping' to the server connection.send('Ping'); // Send the message 'Ping' to the server
@ -155,6 +155,10 @@ connection.onmessage = function (e) {
console.log('Server: ' + e.data); console.log('Server: ' + e.data);
}; };
document.getElementById('start').addEventListener('click', () => {
connection.send('says hello!')
})
const board = document.getElementById('board'); const board = document.getElementById('board');
const squaresPerSide = 20; const squaresPerSide = 20;
const squareSize = 40; const squareSize = 40;

@ -1,11 +1,30 @@
const WebSocket = require('ws'); const WebSocket = require('ws');
const uuid = require('node-uuid');
const wss = new WebSocket.Server({ port: 8080 }); const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) { const DEBUG = true;
ws.on('message', function incoming(message) {
console.log('received: %s', message); 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);
},
};
ws.send('something'); 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