]>
git.proxmox.com Git - pve-xtermjs.git/blob - src/www/main.js
1 console
.log('xtermjs: starting');
20 function updateState(newState
, msg
) {
21 var timeout
, severity
, message
;
23 case states
.connecting
:
24 message
= "Connecting...";
26 severity
= severities
.warning
;
28 case states
.connected
:
29 message
= "Connected";
31 case states
.disconnecting
:
32 message
= "Disconnecting...";
34 severity
= severities
.warning
;
36 case states
.disconnected
:
39 case states
.connecting
:
40 message
= "Connection failed";
42 severity
= severities
.error
;
44 case states
.connected
:
45 case states
.disconnecting
:
46 message
= "Connection closed";
49 case states
.disconnected
:
53 throw "unknown state";
57 throw "unknown state";
60 message
+= " (" + msg
+ ")";
63 showMsg(message
, timeout
, severity
);
66 var terminalContainer
= document
.getElementById('terminal-container');
67 document
.getElementById('status_bar').addEventListener('click', hideMsg
);
71 function createTerminal() {
72 term
= new Terminal();
74 term
.on('resize', function (size
) {
75 if (state
=== states
.connected
) {
76 socket
.send("1:" + size
.cols
+ ":" + size
.rows
+ ":");
80 protocol
= (location
.protocol
=== 'https:') ? 'wss://' : 'ws://';
83 var type
= getQueryParameter('console');
84 var vmid
= getQueryParameter('vmid');
85 var vmname
= getQueryParameter('vmname');
86 var nodename
= getQueryParameter('node');
87 var url
= '/nodes/' + nodename
;
90 url
+= '/qemu/' + vmid
;
93 url
+= '/lxc/' + vmid
;
102 url
: url
+ '/termproxy',
103 success: function(result
) {
104 var port
= encodeURIComponent(result
.data
.port
);
105 ticket
= result
.data
.ticket
;
106 socketURL
= protocol
+ location
.hostname
+ ((location
.port
) ? (':' + location
.port
) : '') + '/api2/json' + url
+ '/vncwebsocket?port=' + port
+ '&vncticket=' + encodeURIComponent(ticket
);
108 term
.open(terminalContainer
, true);
109 socket
= new WebSocket(socketURL
, 'binary');
110 socket
.binaryType
= 'arraybuffer';
111 socket
.onopen
= runTerminal
;
112 socket
.onclose
= stopTerminal
;
113 socket
.onerror
= errorTerminal
;
114 window
.onbeforeunload
= stopTerminal
;
115 updateState(states
.connecting
);
117 failure: function(msg
) {
118 updateState(states
.disconnected
,msg
);
124 function runTerminal() {
125 socket
.onmessage = function(event
) {
126 var answer
= Utf8ArrayToStr(event
.data
);
127 if (state
=== states
.connected
) {
129 } else if(state
=== states
.connecting
) {
130 if (answer
.slice(0,2) === "OK") {
131 updateState(states
.connected
);
132 term
.write(answer
.slice(2));
139 term
.on('data', function(data
) {
140 if (state
=== states
.connected
) {
141 socket
.send("0:" + unescape(encodeURIComponent(data
)).length
.toString() + ":" + data
);
145 ping
= setInterval(function() {
149 window
.addEventListener('resize', function() {
150 clearTimeout(resize
);
151 resize
= setTimeout(function() {
157 socket
.send(PVE
.UserName
+ ':' + ticket
+ "\n");
159 setTimeout(function() {term
.fit();}, 250);
162 function stopTerminal(event
) {
167 updateState(states
.disconnected
, event
.msg
+ event
.code
);
170 function errorTerminal(event
) {
176 updateState(states
.disconnected
, event
.msg
+ event
.code
);