Student.html 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title></title>
  6. </head>
  7. <body>
  8. <h2>学生A</h2>
  9. <div>
  10. <script src="jquery-1.8.2.js"></script>
  11. <script type="text/javascript">
  12. $(function () {
  13. var ws;
  14. //win服务中设置
  15. var url = "ws://127.0.0.1:8200";
  16. $("#btnConnection").click(function () {
  17. if ("WebSocket" in window) {
  18. ws = new WebSocket(url);
  19. }
  20. else if ("MozWebSocket" in window) {
  21. ws = new MozWebSocket(url);
  22. }
  23. else
  24. alert("浏览器版本过低,请升级您的浏览器。\r\n浏览器要求:IE10+/Chrome14+/FireFox7+/Opera11+");
  25. //注册各类回调
  26. ws.onopen = function () {
  27. $("#msg").append("连接服务器成功<br/>");
  28. ws.send("S:学生A已链接");
  29. }
  30. ws.onclose = function () {
  31. $("#msg").append("与服务器断开连接<br/>");
  32. }
  33. ws.onerror = function () {
  34. $("#msg").append("数据传输发生错误<br/>");
  35. }
  36. ws.onmessage = function (receiveMsg) {
  37. $("#msg").append(receiveMsg.data + "<br/>");
  38. }
  39. });
  40. $("#btnSend").click(function () {
  41. var text = $("#txtContent").val();
  42. ws.send("S:" + text);
  43. $("#msg").append("发送信息至服务器: " + "S:" + text + "<br/>");
  44. });
  45. });
  46. </script>
  47. <input id="btnConnection" type="button" value="连接" />
  48. <br />
  49. <textarea cols="100" rows="10" id="txtContent"></textarea>
  50. <br />
  51. <input id="btnSend" type="button" value="发送" />
  52. <hr />
  53. <span id="msg"></span>
  54. </div>
  55. </body>
  56. </html>