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.