Last active
August 15, 2023 20:30
-
-
Save midnightcodr/c6f83e52a642da1189de4d58881fe01c to your computer and use it in GitHub Desktop.
Revisions
-
midnightcodr revised this gist
Mar 7, 2017 . 2 changed files with 10 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ const Promise = require('bluebird') const WebSocket = require('ws') const nbr = 200 const now = () => { return new Date().getTime() @@ -10,11 +10,17 @@ function Client(id) { this.ws = null this.start = function() { const that = this this.ws = new WebSocket('ws://10.1.8.87:8080') //this.ws = new WebSocket('ws://localhost:8080') this.ws.on('open', function() { console.log(`client ${id} connected`) that.send() that.ws.on('message', function(data) { var msg=JSON.parse(data) if(msg.start) { msg.roudntrip=now() - msg.start // roundtrip time in ms data=JSON.stringify(msg) } console.log('received from server: ', data) setTimeout(function() { that.send() @@ -42,4 +48,4 @@ Promise.map(ids, (id) => { return Promise.resolve() }).then(() => { console.log('all up') }) This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,7 @@ const now = () => { wss.on('connection', (ws) => { ws.on('message', function(data) { var msg=JSON.parse(data) msg.took=now() - msg.start // time in ms from client to server ws.send(JSON.stringify(msg)) }) }) -
midnightcodr revised this gist
Mar 7, 2017 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ ``` npm install bluebird ws node server.js node client.js ``` -
midnightcodr revised this gist
Mar 7, 2017 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ npm install bluebird ws node server.js node client.js -
midnightcodr created this gist
Mar 7, 2017 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ const Promise = require('bluebird') const WebSocket = require('ws') const nbr = 100 const now = () => { return new Date().getTime() } function Client(id) { this.ws = null this.start = function() { const that = this this.ws = new WebSocket('ws://localhost:8080') this.ws.on('open', function() { console.log(`client ${id} connected`) that.send() that.ws.on('message', function(data) { console.log('received from server: ', data) setTimeout(function() { that.send() }, Math.random()*1000) }) }) } this.send = function() { const data = { move: { x: Math.random(), y: Math.random() }, start: now() } this.ws.send(JSON.stringify(data)) } } const ids = Array.from(new Array(nbr), (x,i) => i) Promise.map(ids, (id) => { const c = new Client(id) c.start() return Promise.resolve() }).then(() => { console.log('all up') }) This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ const WebSocket = require('ws') const wss = new WebSocket.Server({ port: 8080 }) const now = () => { return new Date().getTime() } wss.on('connection', (ws) => { ws.on('message', function(data) { var msg=JSON.parse(data) msg.took=now() - msg.start ws.send(JSON.stringify(msg)) }) })