function openTerminal(options) {
    //var CONNECT_TIME = 0; // 请求连接次数
    $("#form").hide();
    Rows = parseInt(options.Rows);
    var client = new WSSHClient();
    var base64 = new Base64();
    var term = new Terminal({cols: 80, rows: Rows, screenKeys: true, useStyle: true});

    // 发送客户端数据
    term.on('data', function (data) {
        console.log("xterm data: ");
        console.log(data);
        client.sendClientData(data);
    });
    term.open();
    $('.terminal').detach().appendTo('#term');
    $("#term").show();
    term.write("Connecting...");
    console.debug(options);

    //var interTime = setInterval(client_connect, 1000)
    setTimeout(client_connect, 3000);

    var intervalId = null;

    function client_connect() {
        //  var TIMEINIT = 0; // 初始化时间
        //  var TIMEOUT  = 60 * 15; // 超时时间
        var CONNECTED = false; // 是否连接成功过
        console.log("连接中....");
        console.log(options);

        client.connect({
            onError: function (error) {
                term.write('Error: ' + error + '\r\n');
                console.log('error happened');
            },
            onConnect: function () {
                console.log('connection established');
                // 连接上之后发送初始化数据
                client.sendInitData(options);
                term.focus();
            },
            onClose: function () {
                console.log("连接关闭");
                term.write("\r\nconnection closed");
                if (CONNECTED) {
                    console.log('connection reset by peer');
                    $('term').hide();
                }
            },
            // 当收到服务端返回的数据
            onData: function (data) {
                if (!CONNECTED) {
                    console.log("first connected.");
                    term.write("\r"); //换行
                    term.focus();     //焦点移动到框上
                }
                /*if(interTime){
                   clearInterval(interTime);
                }*/
                CONNECTED = true;

                data = base64.decode(data);
                /* TIMEINIT = 0;*/
                term.write(data);
                console.log('get data:' + data);
            }
        })
    }
}

var charWidth = 6.2;
var charHeight = 15.2;

/**
 * for full screen
 * @returns {{w: number, h: number}}
 */
function getTerminalSize() {
    var width = window.innerWidth;
    var height = window.innerHeight;
    return {
        w: Math.floor(width / charWidth),
        h: Math.floor(height / charHeight)
    };
}


function store(options) {
    window.localStorage.host = options.host
    window.localStorage.port = options.port
    window.localStorage.username = options.username
    window.localStorage.ispwd = options.ispwd
    window.localStorage.password = options.password
}

function check() {
    return validResult["host"] && validResult["port"] && validResult["username"];
}

function connect() {
    var remember = $("#remember").is(":checked")
    var options = {
        host: $("#host").val(),
        port: $("#port").val(),
        username: $("#username").val(),
        password: $("#password").val(),
        Rows: $("#terminalRow").val(),
    }
    if (remember) {
        store(options)
    }
    if (true) {
        openTerminal(options)
    } else {
        for (var key in validResult) {
            if (!validResult[key]) {
                alert(errorMsg[key]);
                break;
            }
        }
    }
}
