forked from brunnolou/Cockpit-Real-time-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
2,952 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
httpPort: 3000, | ||
requestProtocol: "refresh-protocol" | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('./app'); |
Oops, something went wrong.