github.com/fanux/shipyard@v0.0.0-20161009071005-6515ce223235/controller/static/app/containers/exec.controller.js (about) 1 (function(){ 2 'use strict'; 3 4 angular 5 .module('shipyard.containers') 6 .controller('ExecController', ExecController); 7 8 ExecController.$inject = ['$http', '$stateParams', 'ContainerService']; 9 function ExecController($http, $stateParams, ContainerService) { 10 var vm = this; 11 vm.id = $stateParams.id; 12 vm.addr = ""; 13 vm.command = "bash"; 14 vm.connect = connect; 15 vm.disconnect = disconnect; 16 var term; 17 var websocket; 18 19 function connect() { 20 var termWidth = Math.round($(window).width() / 7.5); 21 var termHeight = 30; 22 var cmd = vm.command.replace(" ", ","); 23 24 var url = window.location.href; 25 var urlparts = url.split("/"); 26 var scheme = urlparts[0]; 27 var wsScheme = "ws"; 28 29 if (scheme === "https:") { 30 wsScheme = "wss"; 31 } 32 33 // we make a request for a console session token; this is used 34 // as authentication to make sure the user has console access 35 // for this exec session 36 $http 37 .get('/api/consolesession/' + vm.id) 38 .success(function(data, status, headers, config) { 39 vm.token = data.token; 40 vm.addr = wsScheme + "://" + window.location.hostname + ":" + window.location.port + "/exec?id=" + vm.id + "&cmd=" + cmd + "&h=" + termHeight + "&w=" + termWidth + "&token=" + vm.token; 41 42 if (term != null) { 43 term.destroy(); 44 } 45 46 websocket = new WebSocket(vm.addr); 47 48 websocket.onopen = function(evt) { 49 term = new Terminal({ 50 cols: termWidth, 51 rows: termHeight, 52 screenKeys: true, 53 useStyle: true, 54 cursorBlink: true, 55 }); 56 term.on('data', function(data) { 57 websocket.send(data); 58 }); 59 term.on('title', function(title) { 60 document.title = title; 61 }); 62 term.open(document.getElementById('container-terminal')); 63 websocket.onmessage = function(evt) { 64 term.write(evt.data); 65 } 66 websocket.onclose = function(evt) { 67 term.write("Session terminated"); 68 term.destroy(); 69 } 70 websocket.onerror = function(evt) { 71 if (typeof console.log == "function") { 72 //console.log(evt) 73 } 74 } 75 } 76 }) 77 .error(function(data, status, headers, config) { 78 vm.error = data; 79 }); 80 } 81 82 function disconnect() { 83 if (websocket != null) { 84 websocket.close(); 85 } 86 87 if (term != null) { 88 term.destroy(); 89 } 90 } 91 } 92 })();