From 3f3b8fca401b9a56b446c81895c10c3b8f08f74b Mon Sep 17 00:00:00 2001 From: aymeric <aymeric@feedly.com> Date: Sun, 6 Jan 2019 18:39:44 +0100 Subject: [PATCH] Adds code --- .gitignore | 16 +++++++++++ README.md | 12 +++++++- package-lock.json | 26 ++++++++++++++++++ package.json | 15 ++++++++++ test_ws_client.html | 67 +++++++++++++++++++++++++++++++++++++++++++++ test_ws_server.js | 34 +++++++++++++++++++++++ 6 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 test_ws_client.html create mode 100644 test_ws_server.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..354cd78 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +node_modules/ +venv +__pycache__ +*.pyc + +.vs-code +.idea + +.DS_Store +.DS_Store? +._* + +*~ +*.swp +*.swo +*.log diff --git a/README.md b/README.md index e57bd6f..d0405c7 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,13 @@ # 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 diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..6e27a09 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,26 @@ +{ + "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" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..353f786 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "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" + } +} diff --git a/test_ws_client.html b/test_ws_client.html new file mode 100644 index 0000000..cf1ee55 --- /dev/null +++ b/test_ws_client.html @@ -0,0 +1,67 @@ +<!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> diff --git a/test_ws_server.js b/test_ws_server.js new file mode 100644 index 0000000..07655ef --- /dev/null +++ b/test_ws_server.js @@ -0,0 +1,34 @@ +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"); -- GitLab