当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Server.on方法代码示例

本文整理汇总了TypeScript中ws.Server.on方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Server.on方法的具体用法?TypeScript Server.on怎么用?TypeScript Server.on使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ws.Server的用法示例。


在下文中一共展示了Server.on方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: initialize

export function initialize(server: Server): void {

  log('Starting web socket server')

  const options: IServerOptions = { server }
  const wsServer = new WSServer(options)

  wsServer.on('error', error => {
    log(`Websocket error ${error}`)
  })

  wsServer.on('close', () => {
    log('Connection closed')
  })

  wsServer.on('connection', client => {

    log('Client connection')

    client.on('message', (data, flags) => {
      if (typeof data === 'string') {
        client.send(data.toUpperCase())
      } else {
        client.send(data)
      }

    })

    client.send('data')
    client.send(JSON.stringify({ complex: 'object' }))

  })

}
开发者ID:Edward-Lombe,项目名称:elm-electron,代码行数:34,代码来源:websocket.ts

示例2: test

test('plugins support send', async done => {
  const port = await getPort()
  const server = new WebSocket.Server({ port })

  let capturedSend: any

  // the plugin to extract the send function
  const plugin = reactotron => {
    capturedSend = reactotron.send
    return {}
  }

  // create the client, add the plugin, and connect
  const client = createClient({
    createSocket,
    port,
    onConnect: () => capturedSend(mock.type, mock.payload),
  }).use(plugin)

  // the server waits for the command
  server.on('connection', socket => {
    // fires the server receives a command
    socket.on('message', message => {
      const { type, payload } = JSON.parse(message.toString())
      if (type === 'client.intro') return
      expect(type).toEqual(mock.type)
      expect(payload).toEqual(mock.payload)
      done()
      server.close()
    })
  })

  client.connect()
})
开发者ID:chenrui2014,项目名称:reactotron,代码行数:34,代码来源:plugin-send.test.ts

示例3: before

 before(async () => {
   const serverKeypair = new Key('DKpQPUL4ckzXYdnDRvCRKAm1gNvSdmAXnTrJZ7LvM5Qo', '64EYRvdPpTfLGGmaX5nijLXRqWXaVz8r1Z1GtaahXwVSJGQRn7tqkxLb288zwSYzELMEG5ZhXSBYSxsTsz1m9y8F')
   let nbAsk = 0
   wss = new WebSocketServer({ port: 20903 })
   wss.on('connection', (ws:any) => {
     ws.on('message', (data:any) => {
       const obj = JSON.parse(data)
       if (obj.reqId) {
         ws.send(JSON.stringify({ resId: obj.reqId, body: { bla: 'aa' } }))
       }
       if (obj.auth) {
         if (nbAsk == 1 || nbAsk == 3) {
           const challengeMessage = `WS2P:ACK:gtest:${serverKeypair.pub}:${obj.challenge}`
           const sig = serverKeypair.signSync(challengeMessage)
           if (nbAsk == 1) {
             ws.send(JSON.stringify({ auth: 'ACK', pub: serverKeypair.pub, sig: 'hiohoihio' }))
           }
           if (nbAsk == 3) {
             ws.send(JSON.stringify({ auth: 'ACK', pub: serverKeypair.pub, sig }))
           }
         }
         if (nbAsk == 2) {
           // We do like if the key was wrong
           const clientPub = 'GgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd'
           const challengeMessage = `WS2P:CONNECT:${clientPub}:${obj.challenge}`
           if (!verify(challengeMessage, obj.sig, clientPub)) {
             clientAskError = 'Wrong signature from client CONNECT'
           }
         }
         nbAsk++
       }
     })
   })
 })
开发者ID:duniter,项目名称:duniter,代码行数:34,代码来源:ws2p_connection.ts

示例4: constructor

    constructor() {
        this.board = new ServerBoard();
        this.board.players = [];
        this.board.width = 1000;
        this.board.currentY = 0;
        ServerTimeUtils.start();
        setInterval(() => {
            this.board.tick();
        }, 1000 / 10);

        const wss = new Server({port: 7898});
        console.log('server open');

        wss.on('connection', (ws) => {
            ws.binaryType = "arraybuffer";
            let player = new ServerPlayer(ws);
            this.board.players.push(player);

            ws.on('message', (m: string) => {
                let message = JSON.parse(m) as Message;
                this.board.processMessage(player, message);
            });

            ws.on('close', () => {
                this.board.removePlayer(player);
            });
        });
    }
开发者ID:dested,项目名称:OrbitalCrash,代码行数:28,代码来源:app.ts

示例5: createClosingServer

export function createClosingServer(port: number, onDone?: () => void) {
  const wss = new WebSocket.Server({ port })
  wss.on('connection', () => {
    wss.close()
    onDone && onDone()
  })
}
开发者ID:chenrui2014,项目名称:reactotron,代码行数:7,代码来源:create-closing-server.ts

示例6: Connect

 public Connect(callback: () => void): void {
     this.server = ws.createServer({port: this.chatPort});
     this.server.on("connection", socket => {
         this._addClient(socket);
     });
     callback();
 }
开发者ID:pteyssedre,项目名称:chatty,代码行数:7,代码来源:chatty.ts

示例7: start

    start(floodway: Floodway){

        this.floodway = floodway;

        if(this.config.server == null){
            this.l.debug(`Using separate Server on port ${this.config.port}.`);

            this.server = new Server({
                port: this.config.port,
                verifyClient: this.verifyClient
            });

        }else{
            this.l.debug(`Using already created web-server on port ${  this.config.server.localPort }`);

            this.server = new Server({
                server: this.config.server,
                verifyClient: this.verifyClient.bind(this)
            });

        }

        this.connections = [];

        this.server.on("connection",this.handleConnection.bind(this));

    }
开发者ID:Floodway,项目名称:flood-processor,代码行数:27,代码来源:WebSocketConnector.ts

示例8: Observable

 return new Observable(serverObserver => {
   console.info('started server...');
   let wss = new Server(options);
   wss.on('connection', connection => serverObserver.next(connection));
   return () => {
     wss.close();
   }
 }).share();
开发者ID:LongLiveCHIEF,项目名称:aim,代码行数:8,代码来源:stock_server.ts

示例9: Server

 return new Rx.Observable(serverObserver => {
   console.info('started websocket server at port :' + options.port);  
   let wss = new Server(options);
   wss.on('connection', connection => serverObserver.next(connection));
   return () => {
     wss.close();
   }
 }).share();
开发者ID:ranjitjc,项目名称:ts-express-webapi,代码行数:8,代码来源:ws-server.ts


注:本文中的ws.Server.on方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。