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.
socket = null;
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.
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()
// new code to force the current socket to be closed (note use of 'this' keyword)
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)
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.
Last edited by Lyndel McGee on September 29, 2013 01:12.