<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/public/js/jquery-3.1.1.min.js"></script>
</head>
<body>

<textarea rows="20" cols="200" readonly>

</textarea>

<br>

hello {{.name}}!
<input name="username" data-node="{{.name}}" />
<input type="button" onclick="sendMsg()" value="发送">
</body>
<script type="text/javascript">
    if (window["WebSocket"]) {
        conn = new WebSocket("ws://{{.WebSocketHost}}/websocket");
        conn.onopen = function() {
            console.log("connn websocket")
        };

        conn.onmessage = function(evt) {
            var data = JSON.parse(evt.data);
            if(data.code == 401) {
                location.href = "/login"
            }
            switch(data.Content.Type) {
                case "text_mtype":
                    //addMessage(data.TextMessage);
                    var msg = JSON.parse(data.Content.Data);
                    console.log("收到消息：" + data.Content.Data);
                    $('textarea').val($('textarea').val() + msg.Name + ': ' + msg.Data + "\r\n");
                    break;
                case "OnlineNotice":
                    //updateUsers(data.UserStatus)
                    console.log("新用户上线");
                    break;
                default:
            }
        };

        conn.onerror = function() {
            location.reload()
            alert("连接异常")
        };

        conn.onclose = function() {
            //location.reload()
            alert("连接已断开")
        };
    } else {
        alert("Your browser does not support WebSockets.");
    }

    $('input[name=username]').bind('keypress',function(event){
        if(event.which === 13)
        {
            sendMsg()
        }

    });

    function sendMsg() {
        var name = $('input[name=username]').data('node');
        var msg = $('input[name=username]').val();
        conn.send(JSON.stringify({Type: "text_mtype", Data: msg, Name: name}));
        console.log("发送消息：" + msg)
        $('textarea').val($('textarea').val() + name + ': ' + msg + "\r\n");
        $('input[name=username]').val("")
    }
</script>
</html>
{'avg_line_length': 28.064935064935064, 'max_line_length': 97, 'alphanum_fraction': 0.4812586765386395, 'licenses': ['Apache-2.0'], 'repository_name': 'dingdayu/gochating', 'path': 'templates/index.html', 'size': 2213, 'lang': 'HTML'}