index.html 1.42 KB
Newer Older
Ford committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" charset="utf-8">
        /*
        申请一个WebSocket对象,参数是服务端地址,同http协议使用http:开头一样,
        WebSocket协议的url使用ws://开头,另外安全的WebSocket协议使用wss://开头
        */
        var ws = new WebSocket("ws://localhost:8099/chat/ws?uid=2&word_name=业务经理考核系统&Authld=fc9058b44f7850b4438afb11033891ab");
        ws.onopen = function(){  //当WebSocket创建成功时,触发onopen事件
            console.log("open");
            let message = {
                "type": 0,
                "content": "Hello, this is a test message!"
            };

            ws.send(JSON.stringify(message)); //将消息发送到服务端
        }
        ws.onmessage = function(e){  //当客户端收到服务端发来的消息时,触发onmessage事件,参数e.data包含server传递过来的数据  
            console.log(e.data);
        }
        ws.onclose = function(e){  //当客户端收到服务端发送的关闭连接请求时,触发onclose事件  
            console.log("close");
        }
        ws.onerror = function(e){  //如果出现连接、处理、接收、发送数据失败的时候触发onerror事件  
            console.log(error);
        }
    </script>
</head>
<body>

</body>
</html>