| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <html>
- <head>
- <meta charset="UTF-8">
- <title>websocket测试</title>
- <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
- <style type="text/css">
- h3,h4{
- text-align:center;
- }
- </style>
- </head>
- <body>
- <h3>WebSocket测试,客户端接收到的消息如下:</h3>
- <textarea id = "messageId" readonly="readonly" cols="150" rows="30" >
- </textarea>
- <script type="text/javascript">
- var socket;
- if (typeof (WebSocket) == "undefined") {
- console.log("遗憾:您的浏览器不支持WebSocket");
- } else {
- console.log("恭喜:您的浏览器支持WebSocket");
- //实现化WebSocket对象
- //指定要连接的服务器地址与端口建立连接
- //注意ws、wss使用不同的端口。我使用自签名的证书测试,
- //无法使用wss,浏览器打开WebSocket时报错
- //ws对应http、wss对应https。
- socket = new WebSocket("ws://localhost:8889/plag");
- //连接打开事件
- socket.onopen = function() {
- console.log("Socket 已打开");
- socket.send('plag:{"fileId":16,"baseFileId":10,"fileType":1,"algorithmId":1,"params":null,"sessionId":"0"}');
- };
- //收到消息事件
- socket.onmessage = function(msg) {
- $("#messageId").append(msg.data+ "\n");
- console.log(msg.data );
- };
- //连接关闭事件
- socket.onclose = function() {
- console.log("Socket已关闭");
- };
- //发生了错误事件
- socket.onerror = function() {
- alert("Socket发生了错误");
- }
- //窗口关闭时,关闭连接
- window.unload=function() {
- socket.close();
- };
- }
- </script>
- </body>
- </html>
|