本文整理汇总了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' }))
})
}
示例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()
})
示例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++
}
})
})
})
示例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);
});
});
}
示例5: createClosingServer
export function createClosingServer(port: number, onDone?: () => void) {
const wss = new WebSocket.Server({ port })
wss.on('connection', () => {
wss.close()
onDone && onDone()
})
}
示例6: Connect
public Connect(callback: () => void): void {
this.server = ws.createServer({port: this.chatPort});
this.server.on("connection", socket => {
this._addClient(socket);
});
callback();
}
示例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));
}
示例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();
示例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();