构建Web端更稳定高效的实时通信解决方案。
Use npm/yarn/pnpm
pnpm add client.io
Use WebSocketBucket
import { WebSocketBucket } from 'client.io';
import { jsonStringify2Parse } from 'client.io';
WebSocketBucket.create({
name: 'ACK_WEBSOCKET',
url: 'wss://wss.github.com/test-ws'
heartRate: 5000,
heartbeat: (send) => {
send(
'HeartBeat-client-' + `${new Date().getSeconds()}`,
);
},
receive: (event) => {
if (event.data) {
const data = jsonStringify2Parse(event.data);
if (data.ack && data.msgId) {
setTimeout(() => {
WebSocketBucket.sendMessage(
JSON.stringify({ module: 'ack', msgId: data.msgId }),
'ACK_WEBSOCKET',
);
}, 0);
}
}
return event;
},
send: (event) => {
return event;
},
});
WebSocketBucket.receiveMessage((msgEvent) => {
}, 'ACK_WEBSOCKET');
WebSocketBucket.sendMessage('Hello World!', 'ACK_WEBSOCKET');