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

Login:
Pass:
 
 

Topic:
Polling (TCP)data from server (NOT by RFX unit) - How to utilize socket to read asynchronous events for entire duration of activity.
This thread has 8 replies. Displaying all posts.
Post 1 made on Saturday January 28, 2012 at 10:17
xynyx
Long Time Member
Joined:
Posts:
September 2009
73
What is the (best) way to poll a server for TCP data?
Server sents notifications, whitch i want to pick up.
Do you leave the socket open all the time, waiting for a notification. Ive tried this by opening a async socket but cant/dont get any onData information.

ModEdit - LRM - Added content to the title to make future searches easier.

Last edited by Lyndel McGee (moderator) on January 28, 2012 14:05.
Post 2 made on Saturday January 28, 2012 at 13:49
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,999
Receiving asynchronous events (which is really what you are asking) is typically accomplished by keeping socket connected throughout usage of the activity.

What is the protocol? HTTP? Web Service? You have really not given us enough detail here.
//20120213 - bugfix to assign socket initially so that we can hook callbacks.

var socket;
var buffer = '';

function cleanSocket()
{
if (null === socket)
return;
try
{
if (socket.connected)
socket.close();
}
catch(e)
{
System.print('Error closing socket! e:' + e);
}
socket.onData = null;
socket.onConnect = null;
socket.onIOError = null;
socket.onClose = null;
// release the activity based variable.
socket = null;
}

function onData()
{
buffer += socket.read(); // must read in onDataCall
// process buffer after read to pull off complete messages
// write any messages as needed.
//call cleanSocket() to shutdown as needed.
}

function onIOError(e)
{
System.print('Socket IO Error e:' + e);
cleanSocket();
CF.activity().scheduleAfter(500, doConnect, null);
}

function onClose()
{
System.print('Socket closed from remote side');
cleanSocket();
CF.activity().scheduleAfter(500, doConnect, null);
}

function onConnect(newSocket)
{
// if using socket.listen(), newSocket will be the new client socket just connected.
socket.write('MV?\r'); // this is of your own doing - example is to query denon for master volume.
}

function doConnect()
{
socket = new TCPSocket();
socket.onData = onData;
socket.onIOError = onIOError;
socket.onClose = onClose;
socket.onConnect = onConnect;
socket.connect('192.168.1.55', 23, 3000); // Connect to port 23 on the Denon.
}

doConnect();

Last edited by Lyndel McGee on February 13, 2012 19:07.
Lyndel McGee
Philips Pronto Addict/Beta Tester
OP | Post 3 made on Saturday January 28, 2012 at 17:29
xynyx
Long Time Member
Joined:
Posts:
September 2009
73
Lyndel,
Thnx for the elaborate reply!
Im building my own script for Logitech Media server. The server listens on port 9090 for TCP/IP connections. I can control the squeezebox allready, but getting notifications was proving difficult (for me).
Furthermore i dont use the onClose, because the server doesnt close the socket. It stays open. With "\n" as last character the server signals the end of the reply.
Anyway thnx again. This forum would be dead if it wasn't for you!
(No offence to others, but you get my drift)

jeroen

Shortent code:
function tcpcom(){
if(LMSsocket != null){System.print("Closing socket first");LMSsocket.close(); LMSsocket=null}
LMSsocket = new TCPSocket(false);
LMSsocket.onConnect = function(){LMSsocket.write(command)}
LMSsocket.onIOError = function (e) {GUI.widget("ERRpanel").label = "IOError " + e}
LMSsocket.onData = function(){result += LMSsocket.read(); resultlen = result.length;
if(result.substr(resultlen-1) == "\n"){result = result.substr(0,resultlen-1);
switch(check){
case "Player 0 ID":PlayerID = decodeURIComponent(result.substr (comlen+1)); GUI.widget("L1").visible=true;Terminate();break
//.....
}}}
LMSsocket.connect(LMS.host, LMS.TCPport, LMS.duration);
}

function Terminate(){
if(LMSsocket!=null){LMSsocket.close();LMSsocket=null;}
}

Last edited by xynyx on February 11, 2012 04:42.
Post 4 made on Sunday January 29, 2012 at 10:39
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,999
Why not just pay the $29.95USD to Barry for SqueezePronto or to Nick Riley for ProntoSqueeze (I may have names backward).

I, too, use the CLI interface but do so only for 1-way control.
Lyndel McGee
Philips Pronto Addict/Beta Tester
OP | Post 5 made on Sunday January 29, 2012 at 14:26
xynyx
Long Time Member
Joined:
Posts:
September 2009
73
On January 29, 2012 at 10:39, Lyndel McGee said...
Why not just pay the $29.95USD to Barry for SqueezePronto or to Nick Riley for ProntoSqueeze (I may have names backward).

I, too, use the CLI interface but do so only for 1-way control.

Its a challenge.and programming is my hobby.
OP | Post 6 made on Monday February 13, 2012 at 16:09
xynyx
Long Time Member
Joined:
Posts:
September 2009
73
Lyndell,

Im trying to get your example to work. I understand whats happening, only cant get it to wordk.
Can you give me a page script to approach your script (firing a command at page level). Also the listen() function ???

jeroen
Post 7 made on Monday February 13, 2012 at 18:56
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,999
Put my updated code above either into an activity or into a page that does not repeat. As an exercise, you might want to use the old code and then call System.setDebugMask(9); at the activity level and then put my script into a page. This will help you understand what the original problem was. An error was being thrown as soon as we executed socket.onData = function(){...}. The issue was that socket had not been initialized at this point.

Note that my example ONLY works with a Denon receiver at a particular IP and port 23. You will need to adjust both of these in the doConnect function.

I have corrected a few bugs above to correct my previous issues. Please use the updated code.

Last edited by Lyndel McGee on February 13, 2012 19:09.
Lyndel McGee
Philips Pronto Addict/Beta Tester
OP | Post 8 made on Tuesday February 14, 2012 at 00:51
xynyx
Long Time Member
Joined:
Posts:
September 2009
73
Ill give it a try this week.
Thanks for updating!


One-way Logitech Media server is working (not bug free, but getting there).
Now I only want the elapsed time to appear. Tried active polling with the elapsed time command. But that gives problems with the socket opening and closing correctly. For a time it goes well, but suddenly it skips a close and goes haywire...
OP | Post 9 made on Friday February 17, 2012 at 09:05
xynyx
Long Time Member
Joined:
Posts:
September 2009
73
Implemented your script.
Putting commands in de onConnect call and active play-time polling from the Page script is working stable. First had a lot of "Socket not ready" errors but after changing the script sequence there gone (hopefully forever... ;-) )
Couldnt have pulled it off without your help.
Thx again!
jeroen

Last edited by xynyx on February 22, 2012 10:19.


Jump to


Protected Feature Before you can reply to a message...
You must first register for a Remote Central user account - it's fast and free! Or, if you already have an account, please login now.

Please read the following: Unsolicited commercial advertisements are absolutely not permitted on this forum. Other private buy & sell messages should be posted to our Marketplace. For information on how to advertise your service or product click here. Remote Central reserves the right to remove or modify any post that is deemed inappropriate.

Hosting Services by ipHouse