Methodology | Return Type | Description |
Establish a Socket.IO connection and define the business logic in the callback function. After the callback function has been executed, a Response object will be returned. |
Object | Description |
Optional configuration items when establishing a connection using the connect method. | |
If the connection is successfully established, the created Socket.IO object will be passed into the callback function. You can define your request logic and send/receive event messages in this callback function. | |
After the callback function has been executed, the connect method will return a Response object. |
// SocketIO APIimport socketio from 'pts/socketio';import { check } 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);}, {// Support polling and WebSocket protocols.protocol:'websocket',headers: {token: 'ZER3XSR',}});check('status is 200', () => res.status === 200);}