on(event: string, callback: (...args: any[]) => void): void
Parameter | Type | Description |
event | string | Event. |
callback | function | Callback function. |
Type | Description |
void | No content returned. |
// SocketIO APIimport socketio from 'pts/socketio';import { check, sleep } from 'pts';import util from 'pts/util';export default function () {const res = socketio.connect('http://localhost:8080', function (socket) {socket.on('open', () => console.log('connected'));socket.on('message', (data) => console.log('message received: ', data));socket.on('binaryMessage', (data) => console.log('binaryMessage received: ', data));socket.on('close', () => console.log('disconnected'));socket.setTimeout(function () {console.log('3 seconds passed, closing the socket');socket.close();}, 3000);// Set the scheduled task.socket.setTimeout(function () {socket.emit('message', 'hello');socket.emit('binaryMessage', util.base64Decoding('aGVsbG8=', 'std', 'b'));socket.emit('ackMessage', 'hello ack', function(msg) {console.log('ack message received: ', msg)})}, 500);// Set the periodic task.socket.setInterval(function(){socket.emit('message', 'interval message');}, 500);// Set up a loop to execute tasks, and socket/context will close and naturally exit.socket.setLoop(function () {sleep(0.1);socket.emit('message', 'loop message');});}, {// Support polling and WebSocket protocols.protocol:'websocket',headers: {token: 'xxx',}});check('status is 200', () => res.status === 200);}