Adding to public-facing deploy.

master
Ben Burlingham 5 years ago
parent a7a26a9837
commit 9b62ec7336
  1. 8
      README.md
  2. BIN
      assets/avatar.png
  3. BIN
      assets/gas-giant.png
  4. 3
      client/connection.js
  5. 2
      server/ricochet.js
  6. 48
      socket/messenger.js
  7. 52
      socket/server.js

@ -9,14 +9,10 @@ Any movement, including initial locations, is represented by pushing or popping
A victory state can be stored by taking a snapshot of the current stack.
## Credit
## Credits
Icons from [https://game-icons.net](https://game-icons.net)
## TODO
- chat box
- walls and winstate algorithm
- tutorial
# Final install
- move websocket server to /core
- dynamic socket server resolution
- restore name prompt1

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

@ -30,7 +30,8 @@ const Connection = function() {
};
Connection.prototype.connect = function() {
this.ws = new WebSocket('ws://localhost:8080/ricochet?name=' + this.name);
const hostname = window.location.hostname;
this.ws = new WebSocket(`ws://${hostname}:8080/ricochet?name=${this.name}`);
this.ws.addEventListener('open', this.socketListeners.open);
this.ws.addEventListener('error', this.socketListeners.error);

@ -189,7 +189,7 @@ Ricochet.prototype.freshWalls = function() {
Ricochet.prototype.freshObjective = function() {
const rand = Math.floor(Math.random() * this.robotIds.length);
const id = this.robotIds[0]; //this.robotIds[rand];
const id = this.robotIds[rand];
const { i, j } = this.randomUnoccupiedSquare();
return {

@ -1,48 +0,0 @@
const WebSocket = require('ws');
const DEBUG = (process.env.NODE_ENV !== "production");
const Messenger = function() {
this.clients = {};
};
Messenger.prototype.messageOne = function(ws, message) {
DEBUG && console.log(`Sending to only ${ws.id}:`);
DEBUG && console.log(message);
ws.send(JSON.stringify(message));
};
Messenger.prototype.messageOthers = function(ws, message) {
DEBUG && console.log(`Sending to other client(s):`);
DEBUG && console.log(message);
Object.values(this.clients).forEach((client) => {
if (client !== ws && client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(message));
}
});
};
Messenger.prototype.messageAll = function(message) {
const clients = Object.values(this.clients);
DEBUG && console.log(`Sending to all ${clients.length} client(s):`);
DEBUG && console.log(message);
clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(message));
}
});
};
Messenger.prototype.subscribe = function(ws) {
this.clients[ws.id] = ws;
};
Messenger.prototype.unsubscribe = function(ws) {
delete this.clients[ws.id];
};
module.exports = Messenger;

@ -1,52 +0,0 @@
const WebSocket = require('ws');
const UrlParser = require('url');
const uuid = require('node-uuid');
const Messenger = require('./messenger');
const RicochetApp = require(`${__dirname}/../server/ricochet.js`);
const apps = {
'/ricochet': new RicochetApp({ messenger: new Messenger() })
};
// Make sure all apps follow the messaging contract.
Object.keys(apps).forEach((key) => {
const app = apps[key];
const valid = app && app.messenger && app.onConnect && app.onMessage && app.onDisconnect;
if (!valid) {
delete apps[key];
console.log(`==================== \n\nCRITICAL ERROR! \n\nApplication at ${key} is missing one or more: messenger, onConnect, onMessage, onDisconnect.\n\n====================\n`);
}
});
const onConnect = (ws, req) => {
// Store an ID on the socket connection.
ws.id = uuid.v4();
const url = UrlParser.parse(req.url, true);
const app = apps[url.pathname];
if (app) {
app.messenger.subscribe(ws);
app.onConnect(ws, req);
ws.on('message', (rawBody) => {
app.onMessage(ws, rawBody);
});
ws.on('close', () => {
app.onDisconnect(ws);
app.messenger.unsubscribe(ws);
});
}
};
//===== Generic socket server for any application instantiated above.
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', onConnect);
console.log("Websocket server listening on :8080");
Loading…
Cancel
Save