Subversion Repositories HomeAutomation

Rev

Rev 976 | Blame | Compare with Previous | 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.         Socket.mySockets[this.myId] = this;
  53.     }
  54. }
  55.  
  56. Socket.prototype.stop = function()
  57. {
  58.     if (this.myId != null)
  59.     {
  60.         if (!stopSocketThread(this.myId))
  61.         {
  62.             log("Socket:" + this.myId + "> Socket thread could not be stopped.\n");
  63.         }
  64.        
  65.         delete Socket.mySockets[this.myId];
  66.         this.myId = null;
  67.     }
  68. }
  69.