Hi there,
I am trying to create a TCP connection but there are cases where my app does not get any response from the other end due to failure. So I would like to be notified (actually I want to hide the activity indicator) when waiting for too long. Otherwise it stays there for ever and the app becomes unresponsive!
Example of code is shown below but I cannot post a full working sample because I wouldn't like to post ip, port and stuff like that publicly.
var socket = Ti.Network.Socket.createTCP({ host : ip, port : port, timeout:10000, connected : function(e) { Ti.Stream.pump(e.socket, function(ec){ Ti.API.info('pump waiting...'); readCallback(ec, e.socket); }, 1024, true); var outData = Ti.createBuffer({ value : "some value" }); Ti.Stream.write(e.socket, outData, writeCallback); }, error : function(e) { actInd.hide(); Ti.API.error('Error (' + e.errorCode + '): ' + e.error); alert('Error (' + e.errorCode + '): ' + e.error); } }); var actInd = Ti.UI.createActivityIndicator({ left:0, top:0, right:0, bottom:0, backgroundColor:'black' color:'#fff', style: Ti.UI.iPhone.ActivityIndicatorStyle.PLAIN, message: 'Please wait...', zIndex:1000 }); actInd.show(); if (!Ti.Network.online) { actInd.hide(); alert('no connection!'); } else socket.connect(); function readCallback(e, socket) { if (e.buffer) { var received = e.buffer.toString(); //process data... } else { if (socket.getState() == Ti.Network.Socket.CONNECTED) socket.close(); actInd.hide(); } } function writeCallback(e) { //do nothing... }The problem lies with Ti.Stream.pump(). The timeout I have set when creating the TCP socket (10 secs) does not seem to have any effect.
The scenario is that the TCP connection is successful (connect callback is executed fine) but afterwards something happens on the "server" and I wait forever for receiving something.
How can I add a timeout to pump() function? I would like to avoid a custom timer because I make multiple calls as such concurrently and I cannot handle the syncing between timers.
Thanks in advance for any suggestion.
Application type: mobile Platform: iOS Titanium SDK: 3.2.3.GA Device: iPhone simulator, iPad mini Host Operating System: OSX 10.8.5 Titanium Studio: build 3.2.3.201404181442