wide/static/js/shell.js

61 lines
1.9 KiB
JavaScript
Raw Normal View History

2015-01-01 05:06:33 +03:00
/*
2015-12-30 11:13:43 +03:00
* Copyright (c) 2014-2016, b3log.org & hacpai.com
2015-01-01 05:06:33 +03:00
*
2014-11-12 18:13:14 +03:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2015-01-01 05:06:33 +03:00
*
2014-11-12 18:13:14 +03:00
* http://www.apache.org/licenses/LICENSE-2.0
2015-01-01 05:06:33 +03:00
*
2014-11-12 18:13:14 +03:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2015-01-01 05:06:33 +03:00
*/
2014-11-12 18:13:14 +03:00
2015-12-08 18:48:16 +03:00
/*
* @file shell.js
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.1, Dec 8, 2015
*/
2014-09-05 10:33:43 +04:00
var shell = {
2014-09-30 10:55:04 +04:00
_shellWS: undefined,
2014-09-25 05:46:24 +04:00
_initWS: function () {
2014-12-11 10:32:24 +03:00
shell.shellWS = new ReconnectingWebSocket(config.channel + '/shell/ws?sid=' + config.wideSessionId);
2014-09-30 10:55:04 +04:00
shell.shellWS.onopen = function () {
2014-09-25 05:46:24 +04:00
console.log('[shell onopen] connected');
};
2014-09-30 10:55:04 +04:00
shell.shellWS.onmessage = function (e) {
2014-09-25 05:46:24 +04:00
console.log('[shell onmessage]' + e.data);
var data = JSON.parse(e.data);
if ('init-shell' !== data.cmd) {
$('#shellOutput').val(data.output);
}
};
2014-09-30 10:55:04 +04:00
shell.shellWS.onclose = function (e) {
2014-09-25 05:46:24 +04:00
console.log('[shell onclose] disconnected (' + e.code + ')');
};
2014-09-30 10:55:04 +04:00
shell.shellWS.onerror = function (e) {
2014-09-25 05:46:24 +04:00
console.log('[shell onerror] ' + e);
};
},
init: function () {
this._initWS();
$('#shellInput').keydown(function (event) {
2014-09-05 10:33:43 +04:00
if (13 === event.which) {
var input = {
cmd: $('#shellInput').val()
};
2014-09-30 10:55:04 +04:00
shell.shellWS.send(JSON.stringify(input));
2014-09-05 10:33:43 +04:00
$('#shellInput').val('');
}
2014-09-25 05:46:24 +04:00
});
2014-09-05 10:33:43 +04:00
}
};
2014-09-25 05:46:24 +04:00
$(document).ready(function () {
2014-09-05 10:33:43 +04:00
shell.init();
});