tiws
tiws copied to clipboard
Not possible to get current state
It would be very handy to get the current connection state.
IE: I'd like to check if we're in a connected state before calling ws.close() -- this way I won't have hook.error() pop if I close a closed connection.
What I do is store it based on the events, this way you always have the state available for checking. Something like:
Backbone.socketStatus = 'disconnected';
Backbone.socket = io.connect(Alloy.CFG.url, {
'transports' : ['websocket'],
'reconnect' : true,
'reconnection delay' : 100,
'reconnection limit' : 5000,
'max reconnection attempts' : Infinity,
'query' : 'description=' + Ti.Network.encodeURIComponent(Ti.App.description) + '&guid=' + Ti.App.guid + '&id=' + Ti.App.id + '&name=' + Ti.App.name + '&version=' + Ti.App.version + '&installId=' + Ti.App.installId
});
Backbone.socket.on('connect', function() {
Backbone.socketStatus = 'connected';
});
Backbone.socket.on('connecting', function() {
Backbone.socketStatus = 'connecting';
});
Backbone.socket.on('disconnect', function() {
Backbone.socketStatus = 'disconnected';
});
Backbone.socket.on('connect_failed', function() {
Backbone.socketStatus = 'disconnected';
});
Backbone.socket.on('error', function() {
Backbone.socketStatus = 'disconnected';
});
Backbone.socket.on('reconnect_failed', function() {
Backbone.socketStatus = 'disconnected';
});
Backbone.socket.on('reconnect', function() {
Backbone.socketStatus = 'connected';
});
Backbone.socket.on('reconnecting', function() {
Backbone.socketStatus = 'connecting';
});
Commit 941e15f0a0d5cb60f5d5390da9d1e78282f67ac7 introduced the readyState property to the iOS version of the module, though this needs to be implemented in the android version too