diff --git a/package-lock.json b/package-lock.json
index c868c11..f18ce14 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -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",
diff --git a/package.json b/package.json
index b287536..8bdb420 100644
--- a/package.json
+++ b/package.json
@@ -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"
}
}
diff --git a/ricochet.html b/ricochet.html
index 3024145..639d0bf 100644
--- a/ricochet.html
+++ b/ricochet.html
@@ -66,7 +66,7 @@
-
+
0:42
@@ -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;
diff --git a/server.js b/server.js
index 02e04f6..6b8e02e 100644
--- a/server.js
+++ b/server.js
@@ -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');
-});
\ No newline at end of file
+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")
\ No newline at end of file