]> git.proxmox.com Git - mirror_xterm.js.git/commitdiff
[addon attach] Implement auto-detaching on socket close/error
authorparis <paris@sourcelair.com>
Thu, 18 Sep 2014 09:06:23 +0000 (09:06 +0000)
committerparis <paris@sourcelair.com>
Thu, 18 Sep 2014 09:06:23 +0000 (09:06 +0000)
addons/attach/attach.js

index e7cac84d7189766c5cf620ce719ec18d654018e1..390d0198d17cc48de304f7e712cedbb501f1d9d5 100644 (file)
       bidirectional = (typeof bidirectional == 'undefined') ? true : bidirectional;
       this.socket = socket;
 
-      socket.addEventListener('message', function (ev) {
+      function _getMessage (ev) {
         term.write(ev.data);
-      });
+      }
+        
+      function _sendData (data) {
+        socket.send(data);
+      }
+        
+      function _detach () {
+        term.off('data', _sendData);
+        socket.removeEventListener('message', _getMessage);
+      }
+
+      socket.addEventListener('message', _getMessage);
 
       if (bidirectional) {
-        this.on('data', function (data) {
-          socket.send(data);
-        });
+        this.on('data', _sendData);
       }
+      
+      socket.addEventListener('close', _detach);
+      socket.addEventListener('error', _detach);
     };
 });
\ No newline at end of file