Skip to content

Instantly share code, notes, and snippets.

@PeterHindes
Last active May 25, 2022 22:28
Show Gist options
  • Save PeterHindes/0994d14490036ccda3052d59fa59b700 to your computer and use it in GitHub Desktop.
Save PeterHindes/0994d14490036ccda3052d59fa59b700 to your computer and use it in GitHub Desktop.
Nodejs websockets boilerplate
const async = require('async')
,fs = require('fs')
,http = require('http')
,ws = require('websocket')
;
const APPID = process.env.APPID;
let connections = [];
const WebSocketServer = ws.server
//create a raw http server (this will help us create the TCP which will then pass to the websocket to do the job)
const httpserver = http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write("<h1>Websocket Server</h1>");
response.end();
})
//pass the httpserver object to the WebSocketServer library to do all the job, this class will override the req/res
const websocket = new WebSocketServer({
"httpServer": httpserver
})
httpserver.listen(8081, () => console.log("My server is listening on port 8081"))
//Object to manage connection state
function Handler(con) {
this.con = con;
this.authenticatedAs = -1;
this.authenticate = async function(uname,pass) {
// Your authentication code
}
this.redeemAuth = async function(token) {
// lookup token in db
// set auth to token uid
this.authenticatedAs = 1234; // TODO REMOVE AND REPLACE WITH DB
this.con.send("loginSuc " + this.authenticatedAs);
// if not in db
//this.con.send("loginFail ");
}
}
//when a legit websocket request comes listen to it and get the connection .. once you get a connection thats it!
websocket.on("request", request=> {
const con = request.accept(null, request.origin)
console.log("open");
con.on("close", () => console.log("CLOSED!!!"))
const hand = new Handler(con);
// When someone sends us stuff
con.on("message", message => {
console.log(`${APPID} Received message: ${message.utf8Data}`)
});
con.send(`Connected successfully to server ${APPID}`);
let m = '[50000,50,"Wow You Are Crazy",false,null]';
con.send("Hello World");
connections.push(hand);
})
//client code
//let ws = new WebSocket("ws://localhost:8080");
//ws.onmessage = message => console.log(`Received: ${message.data}`);
//ws.send("Hello! I'm client")
/*
//code clean up after closing connection
subscriber.unsubscribe();
subscriber.quit();
publisher.quit();
*/
{
"name": "src",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"app": "node index.js",
"hotapp": "nodemon index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"async": "^3.2.3",
"fs": "^0.0.1-security",
"http": "^0.0.1-security",
"websocket": "^1.0.34"
}
}
sudo setenforce 0 && docker build --progress=plain --rm -t foo . && docker run -it -v "$PWD"/src:/home/node/app -p 8081:8081 foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment