Commit 3614e587 authored by ibuler's avatar ibuler

[Update] 添加ping机制

parent 5aac324e
This diff is collapsed.
...@@ -22,16 +22,24 @@ export class Socket { ...@@ -22,16 +22,24 @@ export class Socket {
} }
} }
export async function getWsSock(url: string, namespace: string): Promise<Socket> { export async function getWsSock(url: string, namespace: string): Promise<Socket> {
const emitter = new EventEmitter(); const emitter = new EventEmitter();
const events = {}; const events = {
};
let interval;
events[namespace] = { events[namespace] = {
_OnNamespaceConnected: function (ns, msg) { _OnNamespaceConnected: function (ns, msg) {
emitter.emit('connect', ns); emitter.emit('connect', ns);
interval = setInterval(() => ns.emit('ping', ''), 10000);
}, },
_OnNamespaceDisconnect: function (ns, msg) { _OnNamespaceDisconnect: function (ns, msg) {
emitter.emit('disconnect', ns); emitter.emit('disconnect', ns);
if (interval) {
clearInterval(interval);
}
}, },
_OnRoomJoined: function (ns, msg) { _OnRoomJoined: function (ns, msg) {
...@@ -44,9 +52,11 @@ export async function getWsSock(url: string, namespace: string): Promise<Socket> ...@@ -44,9 +52,11 @@ export async function getWsSock(url: string, namespace: string): Promise<Socket>
_OnAnyEvent: function (ns, msg) { _OnAnyEvent: function (ns, msg) {
emitter.emit(msg.Event, msg); emitter.emit(msg.Event, msg);
} },
};
const options = {
reconnect: 5000,
}; };
const options = {reconnect: 5000};
const conn = <neffos.Conn>await neffos.dial(url, events, options); const conn = <neffos.Conn>await neffos.dial(url, events, options);
const nsConn = <neffos.NSConn>await conn.connect(namespace); const nsConn = <neffos.NSConn>await conn.connect(namespace);
const sock = new Socket(nsConn, emitter); const sock = new Socket(nsConn, emitter);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment