Skip to content
Browse files

sync socket/channel state on client after disconnect/reconnect (mainl…

…y an IE issue)
  • Loading branch information...
1 parent f7867d4 commit 911a97fedfc6d07fca4289add89358b4a6e8f7fc @ryedin ryedin committed
Showing with 79 additions and 45 deletions.
  1. +1 −1 api_tester/lib/addChannels.js
  2. +2 −5 lib/channels.js
  3. +76 −39 lib/feather-client/socket.js
View
2 api_tester/lib/addChannels.js
@@ -90,5 +90,5 @@ exports.init = function(feather) {
_cb();
}
}
- })
+ });
};
View
7 lib/channels.js
@@ -195,11 +195,8 @@ Channel.prototype = {
}
client.on("disconnect", function() {
- //TODO
- //IE sends disconnect events when it shouldn't which can cause this session to be removed
- //before it should, so commenting this out for now. This is going to orphan stuff in redis,
- //so it's only a temporary fix
- //me.remove(client);
+ me.remove(client);
+ console.log('client was disconnected -------------------------- clientId: ' + client.id);
});
//lastly, tell the client it has successfully subscribed (and let it know its own channel specific id)
View
115 lib/feather-client/socket.js
@@ -17,6 +17,7 @@
return this.states.connected;
}
},
+
connected: {
stateStartup: function() {
var me = this;
@@ -44,7 +45,18 @@
return this.states.ready;
}
},
- ready: feather.FiniteStateMachine.emptyState
+
+ ready: {
+ disconnect: function() {
+ return this.states.disconnected;
+ }
+ },
+
+ disconnected: {
+ reconnect: function() {
+ return this.states.ready;
+ }
+ }
}
});
@@ -145,9 +157,18 @@
feather.socket.client.on("connect", function() {
feather.socket.stateMachine.fire("connected");
});
+
feather.socket.client.on('message', function(obj){
receive(obj);
});
+
+ feather.socket.client.on('disconnect', function() {
+ feather.socket.stateMachine.fire('disconnect');
+ });
+
+ feather.socket.client.on('reconnect', function() {
+ feather.socket.stateMachine.fire('reconnect');
+ });
}
});
@@ -210,20 +231,52 @@
var channels = new feather.Registry();
var channelClass = function(){
+ var me = this;
+
channelClass._super.apply(this, arguments);
+
+ this.fsm = new feather.FiniteStateMachine({
+ states: {
+ initial: {
+ subscribe: function() {
+ return this.states.subscribed;
+ }
+ },
+
+ subscribed: {
+ unsubscribe: function() {
+ return this.states.initial;
+ }
+ }
+ }
+ });
+
+ this.on('subscribe', function() {
+ me.fsm.fire('subscribe');
+ });
+
+ this.on('unsubscribe', function() {
+ me.fsm.fire('unsubscribe');
+ });
};
channelClass.prototype = {
- send: function(message, data, toClients) {
- feather.socket.send({
- type: "channel",
- data: {
- channelId: this.id,
- message: message,
- toClients: toClients,
- data: data
- }
- });
+ send: function(message, data, toClients) {
+ var me = this;
+
+ //queue up messages until subscribed
+ me.fsm.onceState('subscribed', function() {
+ feather.socket.send({
+ type: "channel",
+ data: {
+ channelId: me.id,
+ message: message,
+ toClients: toClients,
+ data: data
+ }
+ });
+ });
},
+
subscribe: function(data) {
feather.socket.send({
type: "channel",
@@ -234,6 +287,7 @@
}
});
},
+
unsubscribe: function() {
feather.socket.send({
type: "channel",
@@ -243,40 +297,14 @@
}
});
},
- joinGroup: function(groupName) {
- feather.socket.send({
- type: "channel",
- data: {
- channelId: this.id,
- message: "group:" + groupName + ":"
- }
- });
- },
- sendGroup: function(groupName, message, data) {
- feather.socket.send({
- type: "channel",
- data: {
- channelId: this.id,
- message: "group:" + groupName + ":" + message,
- data: data
- }
- });
- },
- leaveGroup: function(groupName) {
- feather.socket.send({
- type: "channel",
- data: {
- channelId: this.id,
- message: "group:" + groupName + "::"
- }
- });
- },
+
dispose: function() {
this.unsubscribe();
channels.remove(this);
channelClass._super.prototype.dispose.apply(this, arguments);
}
};
+
inherits(channelClass, feather.EventPublisher);
feather.socket.subscribe = function(options) {
@@ -284,6 +312,15 @@
if (!channel) {
channel = new channelClass(options);
channels.add(channel);
+
+ //handle socket.io disconnect/reconnects (mainly an IE issue)
+ feather.socket.stateMachine.onState('disconnected', function() {
+ channel.fire('unsubscribe');
+ //need to re-subscribe the channel after reconnect happens
+ feather.socket.stateMachine.onceState('ready', function() {
+ channel.subscribe(options.data);
+ })
+ });
}
channel.subscribe(options.data);
return channel;

0 comments on commit 911a97f

Please sign in to comment.
Something went wrong with that request. Please try again.