Your Universal Remote Control Center
RemoteCentral.com
Philips Pronto Professional Forum - View Post
Up level
Up level
The following page was printed from RemoteCentral.com:

Login:
Pass:
 
 

Original thread:
Post 9 made on Sunday September 29, 2013 at 00:30
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,996
I am having trouble gathering all my thoughts on your issue tonight so apologies if this is confusing. There are quite a few issues happening here.

Here is one issue you should be aware of but it is not the cause of your root problem.

try{
socket.close();
socket = null;
}
catch(e)
{
}

The above code will NOT set the socket to null if the close fails (an error is thrown).


My recommendation for debugging and better understanding what is going on is to robustly issue system.prints whenever you are referencing a socket variable for write, close, connect, create, or set to null. However, there is no 'identifier' on a socket that tells you which one it is. But you can overcome this with a little extra work.

Create an integer counter varibble that starts at 0 that can be used to indicate the number of times you have created a socket. As the 'socket' is a Javascript object, you can add fields to it. Set a counter field on your socket that you can use for debugging. For example, socket_bri.myCounter = ++currCounterValue;

Then, in all places where you are referencing a non-null socket, do a System.print(socket_bri.myCounter);

This will help you better understand the asynchronous socket events and lifecycle and also help you isolate and understand the issue.

I strongly suspect is happening is that you are doing a quick OnRotary turn and generating lots of occurrences where clicks > 3. As your code calls out, your connect timeout is 3000ms. So, on previous click iteration, you created a socket and requested a connect. When the next click iteration comes through, the following condition does not hold true.

if (socket_bri && socket_bri.connected)
{
// the close in here will never take place because not connected. A System.print() will confirm this. :-)
}

socket_bri = null; // Releases the refrence to the variable but the socket is still trying to connect and is never closed... :-)

Once connected, your code is issuing a write to socket_bri which may NOT be the socket that was connected in the OnConnected callback. That is, you might have already reassigned another socket to the variable as OnRotary might be occurring many times within the time it takes for the socket to connect. I might suggest using the 'this' keyword to get the real socket on which the callback is occurring and pass that to your write function as a parameter.

socket_bri.onConnect = function()
{
setBright(this,JSON_commande);
// new code to force the current socket to be closed (note use of 'this' keyword)
if (this.connected)
{
try{this.close()}catch(e){}
}
if (socket_bri === this)
{
// if what is happening is what I believe, this line will rarely be executed. System.print() will confirm.
socket_bri = null;
}
};

I would also change setBright to accept 2 parameters to be sure that you are always writing to the socket that was just connected. The way you have the function defined, it could be writing to an instance of a socket that has not yet fully connected as you are cleared and reset socket_bri in the OnRotary callback.

function setBright(sock, command)
{
if (sock.connected)
sock.write(command);
}


The above suggestions should help to if not almost completely eliminate your maximum socket issue. However, if you turn that scroll wheel very fast, you can still have issues.

All of this will better help you understand the issue. The most robust solution is a code rework once this all sinks in.

Please make these changes and see if my assumptions are correct. At that point, we can discuss how to make this code more bulletproof with some reorganization.

Lyndel

Last edited by Lyndel McGee on September 29, 2013 01:12.
Lyndel McGee
Philips Pronto Addict/Beta Tester


Hosting Services by ipHouse