omorandi / tiws

a very simple Titanium module (iOS / Android) for creating native websockets
131 stars 63 forks source link

Not possible to get current state #6

Open coolacid opened 10 years ago

coolacid commented 10 years ago

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.

HilkoLantinga commented 9 years ago

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';
});
omorandi commented 9 years ago

Commit 941e15f0a0d5cb60f5d5390da9d1e78282f67ac7 introduced the readyState property to the iOS version of the module, though this needs to be implemented in the android version too