當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。