[英]Why does the reconnect function not trigger on manual reconnect Socket.IO
當節點服務器使用socket.disconnect(true);
斷開客戶端連接時socket.disconnect(true);
我使用socket.open()
在客戶端再次手動打開連接。
問題是當socket.open()
被觸發時, socket.on('reconnect', (attemptNumber) => {
函數不是socket.on('connect', () => {
工作正常,但我希望里面的代碼只在服務器關閉連接后重新連接時執行。為什么重新連接功能在手動重新連接時不起作用,我可以做些什么來修復或解決這個問題?
您可以像在這個非常簡單的實現中一樣處理它:
class SocketService {
.
.
.
static registerSocket(opts) {
socket.on('open', () => {
console.log('socket connected')
socket.onclose = (e) => {
console.log('Socket is closed. Reconnect will be attempted in 1.5 second.', e.reason);
setTimeout(() =>
/*
* Try connect again after 1.5s,
* could be improved with a while
* and a number max of retries
*/
SocketService
.registerSocket(opts)
,1500);
};
});
socket.onerror = () => {
// Handle first connection error here
};
}
}
以這種方式實施,你有一些保險。
第一個是確保您正在處理重新連接邏輯,第二個是您也在處理連接錯誤
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.