ussocket.htm 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title></title>
  5. <style type="text/css">
  6. #box { overflow: auto;width: 800px; height: 300px; border: 1px solid #dcdcdc; }
  7. #txtInput { width: 430px; }
  8. .userlogin{width:800px;height: 50px;;}
  9. .userlogin div{float:left;width:120px;}
  10. </style>
  11. <!-- <script src="http://lt.1473.cn/socket.io/socket.io.js" type="text/javascript"></script>-->
  12. <script src="http://127.0.0.1:888/socket.io/socket.io.js" type="text/javascript"></script>
  13. <script type="text/javascript" charset="utf-8" src="http://www.1473.cn/uform.js "></script>
  14. <script type="text/javascript">
  15. //TODO 这里是负责建立热线的代码
  16. var socket = io.connect('http://127.0.0.1:888');
  17. //var socket = io.connect('http://lt.1473.cn');
  18. // TODO 这里负责接收服务端广播消息的代码
  19. socket.on('broadCast', function (r) {
  20. $("#messageArea")[0].innerHTML+="广播消息:"+r.msg+'<br>';
  21. });
  22. function sendMsg() {
  23. var input = document.getElementById('txtInput');
  24. var str = input.value;//获取文本框中的值
  25. msgbox(str);//客户端信息追加到div
  26. //发送到服务端
  27. socket.emit('broadCast', { msg: str });//向服务端发送消息
  28. //console.log('[client]' + str);
  29. input.value = "";
  30. input.focus(); //输入框聚焦
  31. }
  32. //输出信息到div
  33. function msgbox(str) {
  34. var box = document.getElementById('messageArea');
  35. box.innerHTML += str + '<br>';
  36. }
  37. //用户登录,不用注册,输入任何信息均能登录
  38. userLogin=function(){
  39. var username=document.getElementById("usernameinput").value;
  40. //发送到服务端
  41. socket.emit('socketLogin', { msg: username });//向服务端发送消息
  42. }
  43. //在好友区域添加好友
  44. socket.on('addFriend', function (r) {
  45. var _fdiv=$$("div",{"innerHTML":r.msg,"style":{"cssText":"width:200px;height:25px;margin:2px;background-color:gray;"},"onclick":function(){
  46. alert("123");
  47. }},$("#allFriends")[0]);
  48. //$("#allFriends")[0].innerHTML+=r.msg+'<br>';
  49. });
  50. //登录用户重复提示
  51. socket.on('UserNameRepeat', function (r) {
  52. alert("用户名重复");
  53. //$("#allFriends")[0].innerHTML+=r.msg+'<br>';
  54. });
  55. </script>
  56. </head>
  57. <body>
  58. <h2>Chat Client</h2>
  59. <div class="userlogin">
  60. <input id="usernameinput" type='text' >
  61. <input type='button' value="登录" onclick='userLogin();'>
  62. </div>
  63. <div id='box' >
  64. <div id="messageArea" style="width:500px;height: 300px; border-right:1px solid #dcdcdc; ; float: left;">
  65. </div>
  66. <div id="allFriends" style="width:295px;height: 300px; float: left;" ></div>
  67. </div>
  68. <div>
  69. <div id="messageType" style="float:left;width:150px;">广播</div>
  70. <input type='text' id='txtInput' ><input type='button' value='发送消息' onclick='sendMsg();'>
  71. </div>
  72. </body>
  73. </html>