How to use the ws.createWebSocketStream function in ws

To help you get started, we’ve selected a few ws examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github opporty-com / Plasma-Cash / plasma-core / src / child-chain / lib / PN.js View on Github external
};
      client._send = (code, payload) => {
        // console.log("send to ", req.connection.remoteAddress, code);
        seq++;
        if (seq >= 255) seq = 0;
        const countChunk = Math.ceil(payload.length / ChunkSize);

        this.chunks[seq] = {
          code,
          countChunk,
          payload
        };
        client._sendChunk(seq, 0);
      };
      const duplex = WebSocket.createWebSocketStream(client, {compress: false, binary: true});

      duplex.on('error', err => console.log(err));

      client.ostream.pipe(duplex);
      duplex.pipe(client.istream).on('data', packet => {
        const {code, seq, versionProtocol, length, payload, chunkNumber, countChunk} = packet;
        if (code === MESSAGE_CODES.CHUNK_RECEIVE)
          return client._sendChunk(seq, chunkNumber + 1);

        this.emit(MESSAGE_CODE[code], payload, client);
      });

      client.isAlive = true;
      client.on('pong', this._heartbeat.bind(client));
      client.on('error', err => console.log(4, err));
github fastify / fastify-websocket / test / router.js View on Github external
fastify.listen(0, err => {
    let pending = 2
    t.error(err)
    const ws = new WebSocket(
      'ws://localhost:' + (fastify.server.address()).port + '/ws/foo'
    )
    const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
    const ws2 = new WebSocket(
      'ws://localhost:' + (fastify.server.address()).port + '/ws'
    )
    const client2 = WebSocket.createWebSocketStream(ws2, { encoding: 'utf8' })
    t.tearDown(client.destroy.bind(client))
    t.tearDown(client2.destroy.bind(client))

    client.setEncoding('utf8')
    client2.setEncoding('utf8')

    client.once('data', chunk => {
      t.equal(chunk, 'foo')
      client.end()
      if (--pending === 0) t.end()
    })
    client2.once('data', chunk => {
github fastify / fastify-websocket / test / router.js View on Github external
fastify.listen(0, err => {
    t.error(err)
    const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/')
    const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
    t.tearDown(client.destroy.bind(client))

    ws.on('message', message => {
      t.equal(message, 'hi from server')
    })

    ws.on('open', () => {
      ws.send('hi from client')
      client.end()
    })

    ws.on('close', () => {
      t.pass()
    })
  })
})
github fastify / fastify-websocket / test / router.js View on Github external
fastify.listen(0, err => {
    t.error(err)
    const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/baz/echo')
    const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
    t.tearDown(client.destroy.bind(client))

    client.setEncoding('utf8')
    client.write('hello server')

    client.once('data', chunk => {
      t.equal(chunk, 'hello client')
      client.end()
    })
  })
})
github fastify / fastify-websocket / test / router.js View on Github external
fastify.listen(0, err => {
    t.error(err)
    const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port)
    const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
    t.tearDown(client.destroy.bind(client))
    ws.on('close', () => {
      t.pass()
    })
  })
})
github fastify / fastify-websocket / test / router.js View on Github external
fastify.listen(0, err => {
    let pending = 2
    t.error(err)
    const ws = new WebSocket(
      'ws://localhost:' + (fastify.server.address()).port + '/ws/foo'
    )
    const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
    const ws2 = new WebSocket(
      'ws://localhost:' + (fastify.server.address()).port + '/ws'
    )
    const client2 = WebSocket.createWebSocketStream(ws2, { encoding: 'utf8' })
    t.tearDown(client.destroy.bind(client))
    t.tearDown(client2.destroy.bind(client))

    client.setEncoding('utf8')
    client2.setEncoding('utf8')

    client.once('data', chunk => {
      t.equal(chunk, 'foo')
      client.end()
      if (--pending === 0) t.end()
    })
    client2.once('data', chunk => {
      t.equal(chunk, 'empty')
      client2.end()
      if (--pending === 0) t.end()
    })
github fastify / fastify-websocket / test / base.js View on Github external
fastify.listen(0, (err) => {
    t.error(err)

    const clientOptions = { headers: { 'x-custom-header': 'fastify is awesome !' } }
    const ws = new WebSocket('ws://localhost:' + fastify.server.address().port, clientOptions)
    const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
    t.tearDown(() => client.destroy())

    client.setEncoding('utf8')
    client.write('hello')

    client.once('data', (chunk) => {
      t.equal(chunk, 'hello')
      client.end()
    })
  })
})
github opporty-com / Plasma-Cash / plasma-core / src / child-chain / lib / PN.js View on Github external
ws.on('open', () => {
      console.log(`WS OPEN ${ options.uri}`);
      ws.ostream = BD.createEncode(protocol);
      ws.istream = BD.createDecode(protocol);
      const duplex = WebSocket.createWebSocketStream(ws, {compress: false, binary: true, maxPayload: 1000000000});

      duplex.on('error', err => console.log(err));
      ws.ostream.on('error', err => console.log(err));
      ws.istream.on('error', err => console.log(err));


      ws.ostream.pipe(duplex);
      duplex.pipe(ws.istream).on('data', packet => {
        const {code, versionProtocol, length, payload, seq, chunkNumber, countChunk} = packet;
        if (countChunk === 1)
          return this.emit(MESSAGE_CODE[code], payload);

        if (!ws.chunks[seq])
          ws.chunks[seq] = [];

        ws.chunks[seq].push(packet);
github fastify / fastify-websocket / index.js View on Github external
function handleRouting (connection, request) {
    const response = new ServerResponse(request)
    request[kWs] = WebSocket.createWebSocketStream(connection)
    request[kWs].socket = connection
    router.lookup(request, response)
  }

ws

Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js

MIT
Latest version published 4 days ago

Package Health Score

94 / 100
Full package analysis