@@ -7,8 +7,14 @@ Socker = (function() {
77 checkInterval : 10000
88 } ,
99 _url ,
10- _ws ;
10+ _ws ,
11+ _timer ;
1112
13+ /**
14+ * Create a websocket connection.
15+ * @param {string } url The server URL.
16+ * @param {object } opts Optional settings
17+ */
1218 function _connect ( url , opts ) {
1319 _url = url ;
1420 if ( opts ) _extend ( _opts , opts ) ;
@@ -44,7 +50,8 @@ Socker = (function() {
4450 } ;
4551
4652 if ( _opts . keepAlive ) {
47- setInterval ( _checkConnection , _opts . checkInterval ) ;
53+ clearInterval ( _timer ) ;
54+ _timer = setInterval ( _checkConnection , _opts . checkInterval ) ;
4855 }
4956 }
5057
@@ -79,21 +86,36 @@ Socker = (function() {
7986 }
8087 }
8188
89+ /**
90+ * Send an event.
91+ * @param {[type] } name [description]
92+ * @param {[type] } data [description]
93+ * @return {[type] } [description]
94+ */
8295 function _send ( name , data ) {
83- message = {
96+ var message = {
8497 name : name ,
8598 data : data
8699 } ;
87100
88101 _ws . send ( JSON . stringify ( message ) ) ;
89102 }
90103
104+ /**
105+ * Check if the connection is established. If not, try to reconnect.
106+ * @return {[type] } [description]
107+ */
91108 function _checkConnection ( ) {
92109 if ( ! _connected ) {
93110 _connect ( _url , _opts ) ;
94111 }
95112 }
96113
114+ /**
115+ * Utility function for extending an object.
116+ * @param {[type] } obj [description]
117+ * @return {[type] } [description]
118+ */
97119 function _extend ( obj ) {
98120 Array . prototype . slice . call ( arguments , 1 ) . forEach ( function ( source ) {
99121 if ( source ) {
@@ -112,3 +134,6 @@ Socker = (function() {
112134 _self . connect = _connect ;
113135 return _self ;
114136} ) ( ) ;
137+
138+ // COMMON.JS
139+ if ( typeof module != 'undefined' && module . exports ) module . exports = Socker ;
0 commit comments