websocket.html 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Netty WebSocket 时间服务器</title>
  6. </head>
  7. <body>
  8. Netty WebSocket 时间服务器
  9. <br>
  10. <script type="text/javascript">
  11. var socket;
  12. if (!window.WebSocket) {
  13. window.WebSocket = window.MozWebSocket;
  14. }
  15. if (window.WebSocket) {
  16. socket = new WebSocket("ws://192.168.3.96:8088/onlinewebsocket/123123123");
  17. socket.onmessage = function (event) {
  18. var ta = document.getElementById('responseText');
  19. ta.value = "";
  20. ta.value = event.data
  21. };
  22. socket.onopen = function (event) {
  23. var ta = document.getElementById('responseText');
  24. };
  25. socket.onclose = function (event) {
  26. console.log(event)
  27. var ta = document.getElementById('responseText');
  28. ta.value = "";
  29. ta.value = "WebSocket 关闭!";
  30. };
  31. }
  32. else {
  33. alert("抱歉,您的浏览器不支持WebSocket协议!");
  34. }
  35. function send(message) {
  36. if (!window.WebSocket) {
  37. return;
  38. }
  39. if (socket.readyState == WebSocket.OPEN) {
  40. var requestHeader = {'key1': 'value1', 'key2': 'value2'};
  41. socket.send(message);
  42. // socket.send(JSON.stringify({'request_header': requestHeader}));
  43. }
  44. else {
  45. alert("WebSocket连接没有建立成功!");
  46. }
  47. }
  48. </script>
  49. <form onsubmit="return false;">
  50. <input type="text" name="message" value="Netty最佳实践"/>
  51. <br><br>
  52. <input type="button" value="发送WebSocket请求消息" onclick="send(this.form.message.value)" />
  53. <input type="button" value="关闭" onclick="socket.close()" />
  54. <hr color="blue"/>
  55. <h3>服务端返回的应答消息</h3>
  56. <textarea id="responseText" style="width:500px;height:300px;"></textarea>
  57. </form>
  58. </body>
  59. </html>