This commit is contained in:
Kewin Brandsma 2016-10-04 21:43:33 +02:00
parent 5177a71a40
commit c48c203f43
2 changed files with 30 additions and 11 deletions

View file

@ -149,8 +149,8 @@ function connect(){
'close',
function connectionClosed(){
client.log('connection closed' ,client.id , client.path,
client.retriesRemaining, 'tries remaining of', client.config.maxRetries
);
client.retriesRemaining, 'tries remaining of', client.config.maxRetries
);
if(
client.config.stopRetrying ||
@ -190,8 +190,8 @@ function connect(){
client.log('## received events ##');
if(client.config.rawBuffer){
client.publish(
'data',
new Buffer(data,client.config.encoding)
'data',
new Buffer(data,client.config.encoding)
);
if(!client.config.sync){
return;
@ -222,16 +222,18 @@ function connect(){
if (message.type === '__identify') {
client.emit('__identify', {
id: client.config.id
//path: client.path
id: client.config.id,
// TODO: This can't be right....
path: client.config.socketRoot + client.config.appspace + client.config.id
});
continue;
}
client.log('detected event', message.type, message.data);
client.publish(
message.type,
message.data
message.type,
message.data
);
}
@ -244,4 +246,4 @@ function connect(){
);
}
module.exports=Client;
module.exports=Client;

View file

@ -247,7 +247,22 @@ function serverCreated(socket) {
socket
);
} else {
this.on('__identify', function(clientDetails) {
// Wait for handshake
var t = setTimeout(() => this.publish('error', 'Child connection did not finish handshake in time'), 2000);
// Handhake callback function
// Checks if its the same socket instance
var __identifyCb = (clientDetails, _socket) => {
if (_socket !== socket)
return;
// Clear handhake timeout
clearTimeout(t);
// Make sure event is removed
this.off('__identify', __identifyCb);
let id = clientDetails.id,
path = clientDetails.path,
clientConfig = Object.assign(this.config, {id: id, path: path});
@ -261,7 +276,9 @@ function serverCreated(socket) {
});
this.publish('connect', socket, this.of[id]);
}.bind(this));
};
this.on('__identify', __identifyCb);
this.emit(socket, '__identify');
}