The SessionSocketMgr JavaScript class, creates and keeps sockets bounded throughout a session.
SCR_NetworkSessionSocketMgrWrapper manages non-blocking socket servers (bound ports) which can exchange text messages with clients (connected ports). Messages are encoded in utf-8 and respect a protocol. (The protocol encapsulates messages between bytes 0x2 and 0x3 - the ASCII table bytes for start text and end text respectively.)
var serverPort = SessionSocketMgr.bindNewPort();
var serverPortBound = false;
var serverPorts = SessionSocketMgr.boundPorts();
for(i=0; i<serverPorts.length; i++)
{
if (serverPorts[i] == serverPort)
{
serverPortBound = true;
break;
}
}
if (serverPortBound)
{
if ( client.connect( "localhost", serverPort ) )
{
clientSocketId = SessionSocketMgr.acceptAtPort(serverPort, 5000, 10);
if ( 0 < clientSocketId )
{
var msg = "Message to server";
encMsg = '\2' + msg + '\3';
client.send(encMsg)
if (SessionSocketMgr.recv(clientSocketId,5000))
{
MessageLog.
trace(
"Server received: '" + SessionSocketMgr.lastReceivedMsg(clientSocketId) +
"'");
}
else
{
}
}
else
{
}
client.disconnect()
}
else
{
}
}
else
{
}
SessionSocketMgr.removeSocketConnection(clientSocketId);