From a6de5eb22e0882255d5b5ad9881a8b5e72c448f4 Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Fri, 31 Jan 2014 15:37:54 +0100 Subject: [PATCH] Confirm before close if the websocket is still open --- butterfly/routes.py | 4 +++- butterfly/static/coffees/main.coffee | 6 ++++++ butterfly/static/javascripts/main.js | 11 ++++++++++- dev.py | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/butterfly/routes.py b/butterfly/routes.py index 813a8d9..16a39b4 100644 --- a/butterfly/routes.py +++ b/butterfly/routes.py @@ -59,6 +59,8 @@ class TermWebSocket(Route, tornado.websocket.WebSocketHandler): print('User %s not found' % user) else: self.user = user + + butterfly_dir = os.getcwd() try: os.chdir(self.path or self.pw.pw_dir) except: @@ -76,7 +78,7 @@ class TermWebSocket(Route, tornado.websocket.WebSocketHandler): env["COLORTERM"] = "butterfly" env["LOCATION"] = "http://%s:%d/" % ( tornado.options.options.host, tornado.options.options.port) - env["BUTTERFLY_DIR"] = os.getcwd() + env["BUTTERFLY_DIR"] = butterfly_dir env["SHELL"] = self.pw.pw_shell or '/bin/sh' env["PATH"] = '%s:%s' % (os.path.abspath(os.path.join( os.path.dirname(__file__), '..', 'bin')), env.get("PATH")) diff --git a/butterfly/static/coffees/main.coffee b/butterfly/static/coffees/main.coffee index 6aa6647..56545ab 100644 --- a/butterfly/static/coffees/main.coffee +++ b/butterfly/static/coffees/main.coffee @@ -1,5 +1,6 @@ term = ws = null cols = rows = null +quit = false $ = document.querySelectorAll.bind(document) ws_url = 'ws://' + document.location.host + '/ws' + location.pathname @@ -26,12 +27,17 @@ ws.onclose = -> if term term.destroy() console.log "WebSocket closed", arguments + quit = true open('','_self').close() ws.onerror = -> console.log "WebSocket error", arguments ws.onmessage = (event) -> term.write event.data +addEventListener 'beforeunload', -> + if not quit + 'This will exit the terminal session' + addEventListener 'resize', resize = -> main = $('main')[0] fake_term = document.createElement('div') diff --git a/butterfly/static/javascripts/main.js b/butterfly/static/javascripts/main.js index aabab69..5ae8842 100644 --- a/butterfly/static/javascripts/main.js +++ b/butterfly/static/javascripts/main.js @@ -1,10 +1,12 @@ // Generated by CoffeeScript 1.6.3 -var $, cols, resize, rows, term, ws, ws_url; +var $, cols, quit, resize, rows, term, ws, ws_url; term = ws = null; cols = rows = null; +quit = false; + $ = document.querySelectorAll.bind(document); ws_url = 'ws://' + document.location.host + '/ws' + location.pathname; @@ -34,6 +36,7 @@ ws.onclose = function() { term.destroy(); } console.log("WebSocket closed", arguments); + quit = true; return open('', '_self').close(); }; @@ -45,6 +48,12 @@ ws.onmessage = function(event) { return term.write(event.data); }; +addEventListener('beforeunload', function() { + if (!quit) { + return 'This will exit the terminal session'; + } +}); + addEventListener('resize', resize = function() { var eh, ew, fake_term, fake_term_div, fake_term_line, main, main_bb; main = $('main')[0]; diff --git a/dev.py b/dev.py index a926e38..c471470 100755 --- a/dev.py +++ b/dev.py @@ -10,7 +10,7 @@ commands = [ 'coffee -wcb -j butterfly/static/javascripts/main.js ' + ' '.join(glob('butterfly/static/coffees/*.coffee')), 'compass watch butterfly/static', - 'python butterfly.py ' + ' '.join(sys.argv[1:]) + 'python butterfly.server.py ' + ' '.join(sys.argv[1:]) ]