paulon

Client

Feb 21st, 2012
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. <html>
  2. <head>
  3. <script type="text/javascript" src="http://www.google.com/jsapi"></script>
  4. <script type="text/javascript">
  5. google.load("jquery", "1.7.1");
  6. </script>
  7. <title>Socket hello world</title>
  8. <style>
  9. textarea{
  10. width:500px;
  11. height:200px;
  12.  
  13. }
  14. input[type=text]{
  15. width:450px;
  16. }
  17. td{
  18. vertical-align:top;
  19. }
  20. #socket-ids{
  21. overflow-x:hidden;
  22. overflow-y:scroll;
  23. border:1px solid #888;
  24. width:200px;
  25. height:200px;
  26. }
  27. .private-msg{
  28. color:red;
  29. }
  30. </style>
  31. <script src="http://socket.local:8080/socket.io/socket.io.js"></script>
  32. <script>
  33. var socket = io.connect('http://socket.local:8080/');
  34. var client_id;
  35. socket.on('connect', function (data) {
  36.  
  37. socket.on('register',function(data){
  38. client_id = data.client_id;
  39. $('h1[id=title]').html("Socket.IO - " + client_id);
  40. });
  41.  
  42. socket.on('client list', function (data) {
  43. $('div[id=socket-ids]').html("");
  44. for(var i in data.clients){
  45. if(i != client_id){
  46. $('div[id=socket-ids]').append(
  47. "<a href='#' id='users-id'><div>" + i + "</div></a>"
  48. );
  49. }
  50. }
  51. });
  52.  
  53.  
  54. socket.on('private message',function(data){
  55. console.log(data);
  56.  
  57. $('textarea[id=text-lobby]').val(function(i,text){
  58. return text + "Private Message From - " + data.from + ": " + data.msg + "\r";
  59. });
  60.  
  61. });
  62. socket.on('lobby message',function(data){
  63. console.log(data);
  64.  
  65. $('textarea[id=text-lobby]').val(function(i,text){
  66. return text + data.sender_id + ": " + data.msg + "\r";
  67. });
  68. });
  69.  
  70. socket.on('some user disconnected',function(data){
  71. console.log('some user disconnected');
  72. $('div[id=socket-ids]').find('a[id=' + data.disconnected_id + ']').remove();
  73.  
  74. });
  75. });
  76. $(document).ready(function(){
  77. $('input[id=button-send]').click(function(){
  78. var input_msg = $('input[id=input-msg]').val();
  79. if(input_msg != ""){
  80. socket.emit('send to all',input_msg,function(data){
  81. if(data){
  82. $('input[id=input-msg]').val("");
  83. }
  84. });
  85. }
  86. });
  87. $('a[id=users-id]').live('click',function(){
  88.  
  89. var socket_id = $(this).find('div').html();
  90. var pm = prompt("Private Message to " + socket_id ,"Enter your message here:");
  91. if(pm!=""){
  92. socket.emit('private message',{msg:pm,socket_id:socket_id},function(data){
  93. if(data.success){
  94. alert("Message has been sent");
  95. }
  96. });
  97. }
  98. });
  99. });
  100. </script>
  101. </head>
  102. <body>
  103. <h1 id='title'>Socket.IO</h1>
  104.  
  105. <br/><hr/>
  106. <table>
  107. <tbody>
  108. <tr>
  109. <td>
  110. <label>Chatbox</label><br/>
  111. <textarea id='text-lobby' readonly="readonly"></textarea><br />
  112. <input id='input-msg' type='text' /><input id='button-send' type='button' value='Send' />
  113. </td>
  114. <td>
  115. <label>CLient List</label>
  116. <div id='socket-ids'>
  117.  
  118. </div>
  119. </td>
  120. <tr>
  121. </tbody>
  122. </table>
  123. </body>
  124. </html>
Add Comment
Please, Sign In to add comment