Created
July 24, 2017 18:02
-
-
Save cobookman/18639595294eb33b18f6e94eec40cf8a to your computer and use it in GitHub Desktop.
Hello World - Sockets - HTTPS Load Balancer - Nodejs - Sticky Sessions
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
{ | |
"name": "hello-sockets", | |
"version": "0.0.1", | |
"private": true, | |
"dependencies": { | |
"express": "~4.15.3", | |
"socket.io": "~2.0.3" | |
}, | |
"engines": { | |
"node": "4.7.2" | |
} | |
} |
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
<html> | |
<head> | |
</head> | |
<body> | |
<h2>Socket Log</h2> | |
<div id="log"> | |
</div> | |
<script src="/socket.io/socket.io.js"></script> | |
<script> | |
var startTime = Date.now(); | |
var host = { name: "", randomNumber: ""}; | |
var socket = io(); | |
socket.on("connect",function onConnect () { console.log("Connect", arguments); }); | |
socket.on("ack", function onAck(data) { | |
if (host.name.length == 0) { | |
host.name = data.host; | |
host.randomNumber = data.randomNumber; | |
} | |
if (host.name != data.host || host.randomNumber != data.randomNumber) { | |
var endTime = Date.now(); | |
alert("No longer talking to same VM"); | |
alert("No longer session affinity after " + (endTime - startTime) + " ms"); | |
console.error(startTime, endTime, (endTime - startTime)); | |
} else { | |
document.getElementById("log").innerHTML += [ | |
"<p>", | |
"<b>Host: </b>" + data.host, | |
"<b>Random Number: </b>" + data.randomNumber, | |
"<b>Timestamp: </b>" + Date.now(), | |
"</p>" | |
].join("\n"); | |
} | |
}); | |
socket.on("disconnect", function onDisconnect () {console.log("disconnect", arguments); }); | |
var randomNumber = Math.random() * 10000; | |
setInterval(function sendMessages() { | |
socket.emit("ack", { | |
"randomNumber": randomNumber | |
}); | |
}, 5000); | |
</script> | |
</body> | |
</html> |
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
const express = require("express"); | |
const app = express(); | |
const http = require("http").Server(app); | |
const io = require("socket.io")(http); | |
const port = 80; | |
http.listen(port, () => { | |
console.log("Server listening on port %d", port); | |
}); | |
app.use(express.static(__dirname + "/public")); | |
"version": "2.0.3", | |
io.on("connection", (socket) => { | |
console.log("Connection established", socket.handshake.address); | |
var randomNumber = Math.random(); | |
socket.on("ack", (data) => { | |
console.log("Received ack", data); | |
socket.emit("ack", { | |
host: "instance1", | |
randomNumber: randomNumber, | |
originalData: data | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment