2013-11-02 45 views
1

我正在嘗試爲失去連接的客戶端重新建立連接過程。在客戶端js中,我可以檢測到連接丟失並定期嘗試重新連接到服務器。當我重新連接時,雖然服務器將使用新的套接字。Node.js客戶端重新連接的唯一標識符

在客戶端,這是我重新連接邏輯

var socket = io.connect(ip, { 
    'reconnect': true, //will create a new socket on the server 
    'reconnection delay': options.retryInterval, 
    'max reconnection attempts': 10 
}); 

socket.on("disconnect", function() { 
    this.connected = false; 
    this.autoretry(); 
}); 

//establish the connection - similiar to the reconnect method 
socket.emit("connect", this.options); 

//.... 
autoretry: function() { 
    if(this.connected) {return;}; 
    var next = this.__retry *= this.options.retryScalar; 
    this.socket.emit("retry", this.connect.options); 
    return _.delay(this.autoretry, next, this); 
} 

的問題是每個客戶都有它自己的tls連接到外部服務器,我想擔任此連接打開一個短暫的時期;等待客戶端重新連接。

是否有一種很好的方法來唯一標識客戶端在節點中,還是應該在客戶端上創建標識符並將其傳遞給服務器?

回答

1

爲了識別客戶機之前,應該與expressjs會議socket.io工作,每個環節都有唯一的ID,並使用許多其他方法將自身與服務器端,(餅乾,頭..)

當插座對象被傳遞由 socket.id財產

回調io.on('connection',function(socket),獲取用於識別客戶

http://howtonode.org/socket-io-auth

也插座都默認一個唯一的ID丟失連接,腳本嘗試重新連接,請嘗試偵聽reconnect,reconnecting,reconnect_failed事件。

https://github.com/LearnBoost/socket.io/wiki/Exposed-events

+0

是否有辦法來識別服務器上進行重新連接,因爲它仍然具有不同的'id' – megawac

+0

找到我的回答創建一個新的socket:http://stackoverflow.com/questions/9668996/does -socket-io-reconnects-re-run-connect將在瀏覽器上創建一個唯一的客戶端ID。 – megawac