Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-4247] New socket API prevents catching App events once connected

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionInvalid
Resolution Date2011-06-21T09:01:19.000+0000
Affected Version/sRelease 1.7.0
Fix Version/sSprint 2011-25, Release 1.8.0
ComponentsiOS
Labels1.7, ipass1, module_network
Reporterjordi domenech
AssigneeStephen Tramer
Created2011-05-28T05:59:35.000+0000
Updated2017-03-28T21:08:46.000+0000

Description

Although I can open a socket connection with the new API socket.open(), the thread where I create and open the connection is unable to catch events (Ti.App.addEventListener) fired with (Ti.App.fireEvent). See demonstration below. Edit lines 6 & 7 with host+port of ant socket server (I cannot reveal my client's server). The event is neither fired nor catched. BUT, if you comment line 25 you'll see all is working as usual.
var demo = Ti.UI.createWindow({
}),
socket = null;

socket = Titanium.Network.Socket.createTCP({
	host: "server.ip",
	port: "server.socketPort",
	connected: function(e) {
		Ti.API.info("socket: connected");

		var readBuffer = Ti.createBuffer({length:1024}),
		bytesRead = 0;
		while((bytesRead = socket.read(readBuffer))>-1) {
			var stringData = Ti.Codec.decodeString({source: readBuffer, length: bytesRead});
			Ti.API.info("socket: received " + bytesRead + " bytes:" + stringData);
			readBuffer.clear(); // clear the buffer before the next read
		}
		socket.close();
	},
	error: function(e) {
		Ti.API.info("socket: error");
		socket.close();
	}
});
socket.connect();  // comment this, then works as usual

Ti.App.addEventListener('ack', function() {
	Ti.API.info("ack catched");
});

setInterval(function() {
	Ti.API.info("firing ack");
	Ti.App.fireEvent('ack');
}, 3000);


demo.open();
Code also available at [https://gist.github.com/999073]

Comments

  1. Paul Dowsett 2011-05-30

    In order for us to progress this issue, please edit your ticket to include a proper [Use-case](http://wiki.appcelerator.org/display/guides/Contributing+to+Titanium#ContributingtoTitanium-CreatingGoodUsecases). Please also use the latest 1.7.X continuous build, and include the version, date and hash here. Kindly read the [Submitting Bug Reports](http://wiki.appcelerator.org/display/guides/Contributing+to+Titanium#ContributingtoTitanium-SubmittingBugReports) guide before raising tickets. Thank you
  2. jordi domenech 2011-05-30

    Hi, Sorry, I was in a hurry when I wrote the ticket. So, I made a very simple app.js with the use case: https://gist.github.com/999073 Edit lines 6 & 7 with host+port of ant socket server, I could not reveal my client's server. I realize that the event is neither fired nor catched. BUT, if you comment line 25 you'll see all is working as usual. I didn't download the 1.7.X continuous build, my workflow is with github. I forked your repo, fetch and checkout your 1_7_X branch, I mean I have the newest code. Hope this helps, let me know if you anything more, I'm very happy to contribute.
  3. Paul Dowsett 2011-05-30

    jordi, are you saying that you have compiled the SDK? If so, it won't be possible for us to reproduce the same conditions. Hence, would you test it using one of the packages from our [CI Builds](http://wiki.appcelerator.org/display/guides/Continuous+Builds) site? Note that when you launch an app, the Titanium version, date and hash will be logged to the console. Once you have provided this information, I can move this issue to the relevant project. Thanks in advance
  4. Paul Dowsett 2011-05-30

    btw, to confirm, you do put quotes around your host and port lines, don't you?
  5. jordi domenech 2011-05-30

    i do :) actually, it works, I mean the socket is opened and works like a charm, writes/reads data. The problem is with the events. Yes, I compiled the SDK from source. Anyway, I've downloaded the last CI, made a clean build and the problem persists. The console says: [DEBUG] Xcode 4.0.2 (it also happens with 4.0.0) [DEBUG] Build version 4A2002a [INFO] MyApp/1.0 (1.7.0.8be59d3...) I cannot see Ti version, date and hash. I'm using Titanium Developer 1.2.2, as Titanium Studio started to crash for a memory problem (and I'm unable to make it work again) a couple of weeks ago.
  6. Paul Dowsett 2011-05-30

    Thanks for the info, Jordi - moving this ticket to the correct project.
  7. Stephen Tramer 2011-06-21

    This is expected behavior. The issue is that once the socket is connected, socket.read() is called - read() is a blocking call on ALL I/O streams by convention. This means until data is received by the socket, ALL activity in the current JS thread is blocked. The solution is to use the asynchronous Ti.Stream.read() method. Sockets should, except in very rare cases, use asynchronous read/write. Doing otherwise can result in issues such as windows not closing, events not being fired, stale UI, poor interaction, and other issues.
  8. Lee Morris 2017-03-28

    Closing ticket as invalid.

JSON Source