<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>websocket client</title> <script type="text/javascript"> var ws1 = null; var ws2 = null; function myFunction() { ws1 = new WebSocket("ws://192.168.31.134:8100/websocket/testResult/web"); ws1.onmessage = function (evt) { console.log(evt); console.log(evt.data) var context = '<div class="sendMsg">' + 'aflnet:<br/>'+ 'aflnet<h3>'+evt.data+'</h3><br/>'+ ' </div>'; document.getElementById("sendDiv").innerHTML = context; }; ws1.onclose = function () { // 关闭 websocket alert("连接已关闭..."); }; } </script> <body onload="javascript:myFunction()"> <div id="sendDiv"> </div> </body> </html>