<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebSocket Chat</title>
</head>
<body>


    <form onsubmit="return false">
        <h3>WebSocket 聊天室: </h3>
        <textarea id="responseText" rows="10" cols="100">

        </textarea>
        <br>
        <input type="text" name="message" value="Welocme to www.liujijiji.cn">
        <input type="button" value="发送消息" onclick="send(this.form.message.value)">
        <input type="button" onclick="javascript:document.getElementById('responseText').value=''" value="清空聊天记录">
    </form>

    <script type="text/javascript">
        var socket;
        if (!window.WebSocket){
            window.WebSocket = window.MozWebSocket;
        }

        if (window.WebSocket){
            socket = new WebSocket("ws://localhost:8081/websocket");
            var ta = document.getElementById('responseText');
            socket.onmessage = function (event) {
                ta.value = ta.value + '\n' + event.data;
            };
            socket.onopen = function (event) {
                ta.value = "连接开启";
            };
            socket.onclose = function (event) {
                ta.value = ta.value + "连接被关闭";
            };
        } else {
            alert("你的浏览器不支持 WebSocket");
        }

        function send(message) {
            if (!window.WebSocket){
                return;
            }
            if (socket.readyState == WebSocket.OPEN){
                socket.send(message);
            } else {
                alert("连接没有开启");
            }


        }

    </script>
</body>
</html>
