ellisgl

Untitled

Jun 22nd, 2011
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. server.js
  2. -----
  3.  
  4. var io = require('socket.io'),
  5. socket = io.listen(8080);
  6.  
  7. socket.on('connection', function(client)
  8. {
  9. // now we have a client object!
  10. console.log("\r\nConnection accepted.\r\n");
  11.  
  12. client.on('message', function(message)
  13. {
  14. console.log("\r\nRecieved message: " + message + " - from client " + client.sessionId + "\r\n");
  15. });
  16. client.on('disconnect', function()
  17. {
  18. console.log("\r\nConnected " + client.sessionId + "terminated.\r\n");
  19. });
  20. });
  21.  
  22.  
  23. index.html (served by apache)
  24. -----
  25. <html>
  26. <head>
  27. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  28. <title>Comet Test</title>
  29. </head>
  30. <body>
  31. <h1>Socket.io Test</h1>
  32. <div><p id="status">Waiting for input</p></div>
  33. <button id="connect" onClick="connect();"/>Connect</button>
  34. <button id="disconnect" onClick="disconnect();">Disconnect</button>
  35. <button id="send" onClick="send();" />Send Message</button>
  36.  
  37. <script src="http://houseofellis.net:8080/socket.io/socket.io.js" type="text/javascript"></script>
  38. <script type="text/javascript">
  39. function connect()
  40. {
  41. socket.connect('houseofellis.net', {port: 8080, rememberTransport: false});
  42. }
  43.  
  44. function disconnect()
  45. {
  46. socket.disconnect();
  47. }
  48.  
  49.  
  50. function message(data)
  51. {
  52. document.getElementById('message').value = data;
  53. }
  54.  
  55. function status_update(txt)
  56. {
  57. document.getElementById('status').innerHTML = txt;
  58. }
  59.  
  60. function esc(msg)
  61. {
  62. return msg.replace(/</g, '&lt;').replace(/>/g, '&gt;');
  63. }
  64.  
  65. function send()
  66. {
  67. socket.send("Hello Server!");
  68. }
  69.  
  70. // Start the socket
  71. var socket = io.connect('houseofellis.net', {port: 8080, rememberTransport: false});
  72.  
  73. socket.on('message', function(data){ message(data); });
  74. socket.on('connect', function(){ status_update("Connected"); });
  75. socket.on('disconnect', function(){ status_update("Disconnected"); });
  76. socket.on('reconnect', function(){ status_update("Reconnected"); });
  77. socket.on('reconnecting', function( nextRetry ) {
  78. status_update("Reconnecting in " + nextRetry + " seconds"); });
  79. socket.on('reconnect_failed', function(){ message("Reconnect Failed"); });
  80. </script>
  81. </body>
  82.  
  83. Server console when I click on the message button
  84. ----
  85. debug - setting request POST /socket.io/1/xhr-polling/542daf353bd7d06cde45d7800118f9a6?t1308741555720
  86. debug - transport not writeable, not subscribing
  87. debug - xhr received data packet 3:::Hello Server!
  88. debug - got packet
Advertisement
Add Comment
Please, Sign In to add comment