Skip to content

Instantly share code, notes, and snippets.

@zerothabhishek
Created January 6, 2021 12:48
Show Gist options
  • Select an option

  • Save zerothabhishek/4226733197ed5b55ee5ce8ce93b783d9 to your computer and use it in GitHub Desktop.

Select an option

Save zerothabhishek/4226733197ed5b55ee5ce8ce93b783d9 to your computer and use it in GitHub Desktop.

Revisions

  1. zerothabhishek created this gist Jan 6, 2021.
    32 changes: 32 additions & 0 deletions anycable_client.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    const WebSocket = require('ws');

    const URL = "ws://ws.example.org:8080/cable"
    const ORIGIN = "https://example.org"

    function connect(label) {

    const ws = new WebSocket(URL, [], { origin: ORIGIN })

    ws.on('open', function open() {
    console.log("~~~> Connected", label)
    })

    ws.on('message', function incoming(data) {
    console.log("~~~>", label, data)
    })

    ws.on('close', function close() {
    console.log('~~~~> disconnected', label)
    })
    }

    function simulateOne(iteration) {
    const label = "test-" + iteration
    connect(label)
    }

    function simulate(num) {
    for (var i = 0; i < num; i++) {
    (function(iteration) { simulateOne(iteration) })(i)
    }
    }