namespace-index.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Demo Chat</title>
  5. <link href="bootstrap.css" rel="stylesheet">
  6. <style>
  7. body {
  8. padding:20px;
  9. }
  10. .console {
  11. height: 400px;
  12. overflow: auto;
  13. }
  14. .username-msg {color:orange;}
  15. .connect-msg {color:green;}
  16. .disconnect-msg {color:red;}
  17. .send-msg {color:#888}
  18. </style>
  19. <script src="js/socket.io/socket.io.js"></script>
  20. <script src="js/moment.min.js"></script>
  21. <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
  22. <script>
  23. var userName1 = 'user1_' + Math.floor((Math.random()*1000)+1);
  24. var userName2 = 'user2_' + Math.floor((Math.random()*1000)+1);
  25. var chat1Socket = io.connect('http://localhost:9092/chat1');
  26. var chat2Socket = io.connect('http://localhost:9092/chat2');
  27. function connectHandler(parentId) {
  28. return function() {
  29. output('<span class="connect-msg">Client has connected to the server!</span>', parentId);
  30. }
  31. }
  32. function messageHandler(parentId) {
  33. return function(data) {
  34. output('<span class="username-msg">' + data.userName + ':</span> ' + data.message, parentId);
  35. }
  36. }
  37. function disconnectHandler(parentId) {
  38. return function() {
  39. output('<span class="disconnect-msg">The client has disconnected!</span>', parentId);
  40. }
  41. }
  42. function sendMessageHandler(parentId, userName, chatSocket) {
  43. var message = $(parentId + ' .msg').val();
  44. $(parentId + ' .msg').val('');
  45. var jsonObject = {'@class': 'com.corundumstudio.socketio.demo.ChatObject',
  46. userName: userName,
  47. message: message};
  48. chatSocket.json.send(jsonObject);
  49. }
  50. chat1Socket.on('connect', connectHandler('#chat1'));
  51. chat2Socket.on('connect', connectHandler('#chat2'));
  52. chat1Socket.on('message', messageHandler('#chat1'));
  53. chat2Socket.on('message', messageHandler('#chat2'));
  54. chat1Socket.on('disconnect', disconnectHandler('#chat1'));
  55. chat2Socket.on('disconnect', disconnectHandler('#chat2'));
  56. function sendDisconnect1() {
  57. chat1Socket.disconnect();
  58. }
  59. function sendDisconnect2() {
  60. chat2Socket.disconnect();
  61. }
  62. function sendMessage1() {
  63. sendMessageHandler('#chat1', userName1, chat1Socket);
  64. }
  65. function sendMessage2() {
  66. sendMessageHandler('#chat2', userName2, chat2Socket);
  67. }
  68. function output(message, parentId) {
  69. var currentTime = "<span class='time'>" + moment().format('HH:mm:ss.SSS') + "</span>";
  70. var element = $("<div>" + currentTime + " " + message + "</div>");
  71. $(parentId + ' .console').prepend(element);
  72. }
  73. $(document).keydown(function(e){
  74. if(e.keyCode == 13) {
  75. $('#send').click();
  76. }
  77. });
  78. </script>
  79. </head>
  80. <body>
  81. <h1>Namespaces demo chat</h1>
  82. <br/>
  83. <div id="chat1" style="width: 49%; float: left;">
  84. <h4>chat1</h4>
  85. <div class="console well">
  86. </div>
  87. <form class="well form-inline" onsubmit="return false;">
  88. <input class="msg input-xlarge" type="text" placeholder="Type something..."/>
  89. <button type="button" onClick="sendMessage1()" class="btn" id="send">Send</button>
  90. <button type="button" onClick="sendDisconnect1()" class="btn">Disconnect</button>
  91. </form>
  92. </div>
  93. <div id="chat2" style="width: 49%; float: right;" >
  94. <h4>chat2</h4>
  95. <div class="console well">
  96. </div>
  97. <form class="well form-inline" onsubmit="return false;">
  98. <input class="msg input-xlarge" type="text" placeholder="Type something..."/>
  99. <button type="button" onClick="sendMessage2()" class="btn" id="send">Send</button>
  100. <button type="button" onClick="sendDisconnect2()" class="btn">Disconnect</button>
  101. </form>
  102. </div>
  103. </body>
  104. </html>