ws#WebSocket TypeScript Examples

The following examples show how to use ws#WebSocket. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: socket.ts    From WebCord with MIT License 6 votes vote down vote up
/** 
 * Tries to reserve the server at given port.
 * 
 * @returns `Promise`, which always resolves (either to `Server<WebSocket>` on
 *          success or `null` on failure).
 */
async function getServer(port:number) {
    const {WebSocketServer} = await import("ws");
    return new Promise<Server<WebSocket>|null>(resolve => {
        const wss = new WebSocketServer({ host: '127.0.0.1', port });
        wss.once('listening', () => resolve(wss));
        wss.once('error', () => resolve(null));
    }) 
}
Example #2
Source File: admin.ts    From hoprnet with GNU General Public License v3.0 6 votes vote down vote up
public onConnection(socket: WebSocket) {
    socket.on('message', (message: string) => {
      debugLog('Message from client', message)
      this.logs.logFullLine(`admin > ${message}`)

      if (this.cmds) {
        this.cmds.execute((resp: string) => {
          if (resp) {
            // Strings may have ansi stuff in it, get rid of it:
            resp = stripAnsi(resp)
            this.logs.logFullLine(resp)
          }
        }, message.toString())
      }
    })

    socket.on('error', (err: string) => {
      debugLog('Error', err)
      this.logs.log('Websocket error', err.toString())
    })
    this.logs.subscribe(socket)
  }