Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Blame | Last modification | View Log | SVN | RSS feed

  1.  
  2. function Socket(callback, address, port, reconnectTimeout)
  3. {
  4.     this.myCallback = callback;
  5.     this.myReconnectTimeout = reconnectTimeout;
  6.     this.myAddress = address;
  7.     this.myPort = port;
  8. }
  9.  
  10. Socket.mySockets = new Array();
  11. Socket.prototype.myId = null;
  12. Socket.prototype.myReconnectTimeout = null;
  13. Socket.prototype.myCallback = null;
  14. Socket.prototype.myAddress = null;
  15. Socket.prototype.myPort = null;
  16.  
  17. Socket.triggerSocketCallback = function(id, event, data)
  18. {
  19.     if (Socket.mySockets[id])
  20.     {
  21.         Socket.mySockets[id].triggerCallback(event, data);
  22.     }
  23.     else
  24.     {
  25.         log("Socket:" + id + "> Encountered a thread that have no javascript Socket object. This is not supposed to be possible, report this.\n");
  26.        
  27.         if (!stopSocketThread(id))
  28.         {
  29.             log("Socket:" + id + "> Socket thread could not be stopped.\n");
  30.         }
  31.     }
  32. }
  33.  
  34. Socket.prototype.triggerCallback = function(event, data)
  35. {
  36.     this.myCallback(event, data);
  37. }
  38.  
  39. Socket.prototype.send = function(data)
  40. {
  41.     if (this.myId != null)
  42.     {
  43.         sendToSocketThread(this.myId, data);
  44.     }
  45. }
  46.  
  47. Socket.prototype.start = function()
  48. {
  49.     if (this.myId == null)
  50.     {
  51.         this.myId = startSocketThread(this.myAddress, this.myPort, this.myReconnectTimeout)
  52.        
  53.         if (this.myId == 0)
  54.         {
  55.             return false;
  56.         }
  57.        
  58.         Socket.mySockets[this.myId] = this;
  59.     }
  60.    
  61.     return true;
  62. }
  63.  
  64. Socket.prototype.stop = function()
  65. {
  66.     if (this.myId != null)
  67.     {
  68.         if (!stopSocketThread(this.myId))
  69.         {
  70.             log("Socket:" + this.myId + "> Socket thread could not be stopped.\n");
  71.         }
  72.        
  73.         delete Socket.mySockets[this.myId];
  74.         this.myId = null;
  75.     }
  76. }
  77.