Skip to content

Commit

Permalink
With websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
brunnolou committed Nov 20, 2017
1 parent 0eb282d commit 46e5790
Show file tree
Hide file tree
Showing 8 changed files with 2,952 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
httpPort: 3000,
requestProtocol: "refresh-protocol"
};
97 changes: 97 additions & 0 deletions app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const config = require("./config");
const http = require("http");
const express = require("express");
const app = express();
const httpServer = app.listen(config.httpPort);

// require('./wsServer')(httpServer);
const WebSocketServer = require("websocket").server;

const wsServer = new WebSocketServer({
httpServer,
// You should not use autoAcceptConnections for production
// applications, as it defeats all standard cross-origin protection
// facilities built into the protocol and the browser. You should
// *always* verify the connection's origin and decide whether or not
// to accept it.
autoAcceptConnections: false
});

function originIsAllowed(origin) {
// put logic here to detect whether the specified origin is allowed.
return true;
}

globalCounter = 0;
allActiveConnections = {};

wsServer.on("request", function(request) {
if (!originIsAllowed(request.origin)) {
// Make sure we only accept requests from an allowed origin
request.reject();
console.log(
new Date() + " Connection from origin " + request.origin + " rejected."
);
return;
}

const connection = request.accept("refresh-protocol", request.origin);

var id = globalCounter++;
allActiveConnections[id] = connection;
connection.id = id;

connection.sendUTF(
JSON.stringify({
action: "connected"
})
);

// setInterval(() => {
// connection.sendUTF(
// JSON.stringify({
// action: "ping"
// })
// );
// }, 1500);

console.log(new Date() + " Connection accepted.");

connection.on("message", function(message) {
if (message.type === "utf8") {
console.log("Received Message: " + message.utf8Data);

connection.sendUTF(
JSON.stringify({
action: "connected",
message: message.utf8Data
})
);
}
});

connection.on("close", function(reasonCode, description) {
console.log(
new Date() + " Peer " + connection.remoteAddress + " disconnected."
);

delete allActiveConnections[connection.id];
});
});

app.use(express.static("public"));
app.all("/refresh/:collection?/:id?", function(req, res, next) {
const { collection, id } = req.params;

Object.values(allActiveConnections).forEach(connection => {
connection.sendUTF(
JSON.stringify({
action: "refreshed",
collection,
id
})
);
});

res.end();
});
69 changes: 69 additions & 0 deletions app/wsServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const WebSocketServer = require("websocket").server;

module.exports = httpServer => {
const wsServer = new WebSocketServer({
httpServer,
// You should not use autoAcceptConnections for production
// applications, as it defeats all standard cross-origin protection
// facilities built into the protocol and the browser. You should
// *always* verify the connection's origin and decide whether or not
// to accept it.
autoAcceptConnections: false
});

function originIsAllowed(origin) {
// put logic here to detect whether the specified origin is allowed.
return true;
}

globalCounter = 0;
allActiveConnections = {};

wsServer.on("request", function(request) {
if (!originIsAllowed(request.origin)) {
// Make sure we only accept requests from an allowed origin
request.reject();
console.log(
new Date() + " Connection from origin " + request.origin + " rejected."
);
return;
}

const connection = request.accept("refresh-protocol", request.origin);

var id = globalCounter++;
allActiveConnections[id] = connection;
connection.id = id;

connection.sendUTF(
JSON.stringify({
action: "connected"
})
);

console.log(new Date() + " Connection accepted.");

connection.on("message", function(message) {
if (message.type === "utf8") {
console.log("Received Message: " + message.utf8Data);

connection.sendUTF(
JSON.stringify({
action: "connected",
message: message.utf8Data
})
);
}
});

connection.on("close", function(reasonCode, description) {
console.log(
new Date() + " Peer " + connection.remoteAddress + " disconnected."
);

delete allActiveConnections[connection.id];
});
});

return wsServer;
};
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./app');
Loading

0 comments on commit 46e5790

Please sign in to comment.