Skip to content

Instantly share code, notes, and snippets.

@midnightcodr
Last active August 15, 2023 20:30
Show Gist options
  • Save midnightcodr/c6f83e52a642da1189de4d58881fe01c to your computer and use it in GitHub Desktop.
Save midnightcodr/c6f83e52a642da1189de4d58881fe01c to your computer and use it in GitHub Desktop.

Revisions

  1. midnightcodr revised this gist Mar 7, 2017. 2 changed files with 10 additions and 4 deletions.
    12 changes: 9 additions & 3 deletions client.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    const Promise = require('bluebird')
    const WebSocket = require('ws')
    const nbr = 100
    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://localhost:8080')
    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')
    })
    })
    2 changes: 1 addition & 1 deletion server.js
    Original 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
    msg.took=now() - msg.start // time in ms from client to server
    ws.send(JSON.stringify(msg))
    })
    })
  2. midnightcodr revised this gist Mar 7, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    ```
    npm install bluebird ws
    node server.js
    node client.js
    node client.js
    ```
  3. midnightcodr revised this gist Mar 7, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    npm install bluebird ws
    node server.js
    node client.js
  4. midnightcodr created this gist Mar 7, 2017.
    45 changes: 45 additions & 0 deletions client.js
    Original 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')
    })
    16 changes: 16 additions & 0 deletions server.js
    Original 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))
    })
    })