Commit 3f3b8fca authored by Aymeric Bernard's avatar Aymeric Bernard
Browse files

Adds code

parent 4257a4bd
node_modules/
venv
__pycache__
*.pyc
.vs-code
.idea
.DS_Store
.DS_Store?
._*
*~
*.swp
*.swo
*.log
# WebSocket RTT Test
Test for Round Trip Time in websocket
\ No newline at end of file
Test for Round Trip Time in websocket
## How to use
Server-side:
- Install dependencies with `npm install`
- Run server with `node test_ws_server.js`
Client-side:
- Change the server IP address to point towards the server
- Open the `test_ws_client.html` file in you web browser
{
"name": "server",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"async-limiter": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz",
"integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg=="
},
"perf_hooks": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/perf_hooks/-/perf_hooks-0.0.1.tgz",
"integrity": "sha512-qG/D9iA4KDme+KF4vCObJy6Bouu3BlQnmJ8jPydVPm32NJBD9ZK1ZNgXSYaZKHkVC1sKSqUiLgFvAZPUiIEnBw=="
},
"ws": {
"version": "6.1.2",
"resolved": "https://registry.npmjs.org/ws/-/ws-6.1.2.tgz",
"integrity": "sha512-rfUqzvz0WxmSXtJpPMX2EeASXabOrSMk1ruMOV3JBTBjo4ac2lDjGGsbQSyxj8Odhw5fBib8ZKEjDNvgouNKYw==",
"requires": {
"async-limiter": "~1.0.0"
}
}
}
}
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "test_ws_server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"perf_hooks": "0.0.1",
"ws": "^6.1.2"
}
}
<!DOCTYPE HTML>
<html>
<head>
<title>WS RTT Client</title>
<script type = "text/javascript">
let ws;
let lastPing = 0;
let lastPong = 0;
function WebSocketTest() {
if ("WebSocket" in window) {
// alert("WebSocket is supported by your Browser!");
console.log("WebSocket is supported by your Browser!");
// Let us open a web socket
// ws = new WebSocket("ws://192.168.0.163:1337/");
ws = new WebSocket("ws://localhost:1337/");
ws.onopen = function() {
// Web Socket is connected, send data using send()
ws.send("BONJOUR");
console.log("BONJOUR sent");
};
ws.onmessage = function (evt) {
const received_msg = evt.data;
// alert("Message is received...");
console.log("Message received: " + received_msg);
if (received_msg === "ping") {
ws.send("pong");
console.log("responded with pong");
} else if (received_msg === "pong") {
console.log("pong received");
lastPong = performance.now();
console.log("lastPong - lastPing = " + (lastPong - lastPing));
}
};
ws.onclose = function() {
// websocket is closed.
// alert("Connection is closed...");
console.log("Connection closed");
};
} else {
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
}
function Ping() {
lastPing = performance.now();
ws.send("ping");
}
</script>
</head>
<body>
<div id = "sse">
<a href = "javascript:WebSocketTest()">Run WebSocket</a>
</div>
<a href = "javascript:Ping()">Ping</a>
</body>
</html>
const WebSocket = require('ws');
const { performance } = require('perf_hooks');
const wss = new WebSocket.Server({ port: 1337 });
let ws;
let lastPing = 0;
let lastPong = 0;
let opened = false;
wss.on('connection', _ws => {
ws = _ws;
opened = true;
ws.on('message', message => {
// console.log(`Received message => ${message}`);
if (message === "ping") {
ws.send("pong");
} else if (message === "pong") {
lastPong = performance.now();
console.log("lastPong - lastPing = " + (lastPong - lastPing));
}
});
});
let sendPing = () => {
if (opened) {
ws.send("ping");
lastPing = performance.now();
}
};
setInterval(sendPing, 1000);
console.log("Server listening");
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment