U.Socket.htm 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>聊天室</title>
  5. <!-- 这里 -->
  6. <script src="http://socketio.1473.cn/socket.io/socket.io.js"></script>
  7. <script src="http://socketio.1473.cn/socket.js"></script>
  8. <!-- 到这里调用socket的时候必要的 -->
  9. <style>
  10. *
  11. {
  12. padding: 0px;
  13. margin: 0px;
  14. }
  15. #m div
  16. {
  17. font: 18px 微软雅黑;
  18. height: 36px;
  19. line-height: 36px;
  20. }
  21. #m div:nth-child(odd)
  22. {
  23. background: #eee;
  24. }
  25. </style>
  26. </head>
  27. <body style="height: 100%;">
  28. <div id="xxx" style="width: 220px; height: 30px; background: #d6d6d6; position: absolute;
  29. top: 40%; left: 45%; font: 16px 微软雅黑; text-align: center; line-height: 30px;
  30. border: 1px solid #d6d6d6;">
  31. 名字
  32. <button style="width: 20%; height: 30px; float: right;" onclick="queding();">
  33. 确定</button>
  34. <input id="n" style="width: 60%; padding: 0px; border: 0px; height: 30px; float: right;" />
  35. </div>
  36. <div id="m" style="width: 100%; height: 600px; background: white; border: 1px solid #d6d6d6;
  37. overflow: auto;">
  38. </div>
  39. <input id="t" onkeydown="sens()" style="width: 80%; height: 40px; float: left; font: 24px 微软雅黑;" />
  40. <div id="b" onclick="send();" style="width: 19.7%; height: 40px; float: left; background: #2d2d2d;
  41. color: White; font: 24px 微软雅黑; text-align: center; line-height: 40px;">
  42. 发送</div>
  43. </body>
  44. <script type="text/javascript">
  45. function jieshou(msg) {
  46. var m = document.getElementById("m");
  47. if (msg.type === "send") {
  48. m.innerHTML += "<div>" + msg.from + ":" + msg.data + "</div>";
  49. }
  50. if (msg.type === "join") {
  51. m.innerHTML += "<div>" + msg.from + "加入了房间!</div>";
  52. }
  53. if (msg.type === "exit") {
  54. m.innerHTML += "<div>" + msg.from + "退出了房间!</div>";
  55. }
  56. m.scrollTop = m.scrollHeight
  57. }
  58. function sens(e) { e = e || event; if (e.keyCode === 13) send(); }
  59. function send() {
  60. var t = document.getElementById("t");
  61. t = t.value;
  62. if (t != "") {
  63. document.getElementById("t").value = "";
  64. socket.send(t, "aa");
  65. }
  66. }
  67. function queding(e) {
  68. socket = new socket(document.getElementById("n").value);
  69. socket.join("aa", jieshou);
  70. document.body.removeChild(document.getElementById("xxx"));
  71. window.onbeforeunload = function () {
  72. socket.exit('aa');
  73. return false;
  74. }
  75. }
  76. </script>
  77. </html>