Subversion Repositories HomeAutomation

Rev

Rev 1962 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. var Socket_Connections = {};
  3.  
  4. var SOCKET_STATE_CONNECTED      = 0x00;
  5. var SOCKET_STATE_DISCONNECTED   = 0x01;
  6. var SOCKET_STATE_ACCEPTED       = 0x02;
  7.  
  8. function Socket_OnNewData(id, data)
  9. {
  10.   if (Socket_Connections[id])
  11.   {
  12.     Socket_Connections[id]["ondata"](id, data);
  13.   }
  14.  
  15.   return true;
  16. }
  17.  
  18. function Socket_OnNewClient(id, server_id)
  19. {
  20.   if (Socket_Connections[server_id])
  21.   {
  22.     Socket_Connections[id] = { "ondata" : Socket_Connections[server_id]["ondata"], "onstate" : Socket_Connections[server_id]["onstate"] };
  23.    
  24.     Socket_Connections[server_id]["onaccept"](id, server_id);
  25.   }
  26.  
  27.   return true;
  28. }
  29.  
  30. function Socket_OnNewState(id, state)
  31. {
  32.   if (Socket_Connections[id])
  33.   {
  34.     Socket_Connections[id]["onstate"](id, state);
  35.    
  36.     if (state == SOCKET_STATE_DISCONNECTED)
  37.     {
  38.       delete Socket_Connections[id];
  39.     }
  40.   }
  41.  
  42.   return true;
  43. }
  44.  
  45. function Socket_StartServer(port, onaccept_callback, ondata_callback, onstate_callback)
  46. {
  47.   socket_id = SocketExport_StartServer(port);
  48.  
  49.   if (socket_id > 0)
  50.   {
  51.     Socket_Connections[socket_id] = { "onaccept" : onaccept_callback, "ondata" : ondata_callback, "onstate" : onstate_callback };
  52.   }
  53.  
  54.   return socket_id;
  55. }
  56.  
  57. function Socket_Connect(address, port, ondata_callback, onstate_callback)
  58. {
  59.   socket_id = SocketExport_Connect(address, port);
  60.  
  61.   if (socket_id > 0)
  62.   {
  63.     Socket_Connections[socket_id] = { "ondata" : ondata_callback, "onstate" : onstate_callback };
  64.   }
  65.  
  66.   return socket_id;
  67. }
  68.  
  69. function Socket_StopServer(socket_id)
  70. {
  71.   SocketExport_StopServer(socket_id);
  72.  
  73.   delete Socket_Connections[socket_id];
  74. }
  75.  
  76. function Socket_Disconnect(socket_id)
  77. {
  78.   SocketExport_Disconnect(socket_id);
  79.  
  80.   delete Socket_Connections[socket_id];
  81. }
  82.  
  83. function Socket_Send(socket_id, data)
  84. {
  85.   SocketExport_Send(socket_id, data);
  86. }
  87.