PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/js/lib/Socket.IO-node/README.md

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
Markdown | 229 lines | 135 code | 94 blank | 0 comment | 0 complexity | 0cc5056b2062608942fa88c728b77a14 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. Socket.IO Server: Sockets for the rest of us
  2. ============================================
  3. The `Socket.IO` server provides seamless support for a variety of transports intended for realtime communication.
  4. - WebSocket
  5. - WebSocket over Flash (+ XML security policy support)
  6. - XHR Polling
  7. - XHR Multipart Streaming
  8. - Forever Iframe
  9. - JSONP Polling (for cross domain)
  10. ## Requirements
  11. - Node v0.1.103+ with `crypto` module support (make sure you have OpenSSL
  12. headers when installing Node to get it)
  13. - The [Socket.IO client](http://github.com/LearnBoost/Socket.IO), to connect from the browser
  14. ## How to use
  15. To run the demo, execute the following:
  16. git clone git://github.com/LearnBoost/Socket.IO-node.git socket.io
  17. cd socket.io/example/
  18. sudo node server.js
  19. and point your browser to `http://localhost:8080`. In addition to `8080`, if the transport `flashsocket` is enabled, a server will be initialized to listen for requests on port `843`.
  20. ### Implementing it on your project
  21. `Socket.IO` is designed not to take over an entire port or Node `http.Server` instance. This means that if you choose to have your HTTP server listen on port `80`, `socket.io` can intercept requests directed to it, and normal requests will still be served.
  22. By default, the server will intercept requests that contain `socket.io` in the path / resource part of the URI. You can change this as shown in the available options below.
  23. On the server:
  24. var http = require('http'),
  25. io = require('./path/to/socket.io'),
  26. server = http.createServer(function(req, res){
  27. // your normal server code
  28. res.writeHead(200, {'Content-Type': 'text/html'});
  29. res.end('<h1>Hello world</h1>');
  30. });
  31. server.listen(80);
  32. // socket.io, I choose you
  33. var socket = io.listen(server);
  34. socket.on('connection', function(client){
  35. // new client is here!
  36. client.on('message', function(){ … })
  37. client.on('disconnect', function(){ … })
  38. });
  39. On the client:
  40. <script src="/socket.io/socket.io.js"></script>
  41. <script>
  42. var socket = new io.Socket();
  43. socket.connect();
  44. socket.on('connect', function(){ … })
  45. socket.on('message', function(){ … })
  46. socket.on('disconnect', function(){ … })
  47. </script>
  48. The [client-side](http://github.com/learnboost/socket.io) files are served automatically by `Socket.IO-node`.
  49. ## Documentation
  50. ### Listener
  51. io.listen(<http.Server>, [options])
  52. Returns: a `Listener` instance
  53. Public Properties:
  54. - *server*
  55. An instance of _process.http.Server_.
  56. - *options*
  57. The passed-in options, combined with the defaults.
  58. - *clients*
  59. An object of clients, indexed by session ID.
  60. Methods:
  61. - *addListener(event, ?)*
  62. Adds a listener for the specified event. Optionally, you can pass it as an option to `io.listen`, prefixed by `on`. For example: `onClientConnect: function(){}`
  63. - *removeListener(event, ?)*
  64. Removes a listener from the listener array for the specified event.
  65. - *broadcast(message, [except])*
  66. Broadcasts a message to all clients. Optionally, you can pass a single session ID or array of session IDs to avoid broadcasting to, as the second argument.
  67. Options:
  68. - *resource*
  69. socket.io
  70. The resource is what allows the `socket.io` server to identify incoming connections from `socket.io` clients. Make sure they're in sync.
  71. - *flashPolicyServer*
  72. true
  73. Create a Flash Policy file server on port `843` (this is restricted port and you will need to have root permission). If you disable the FlashPolicy file server, Socket.IO will automatically fall back to serving the policy file inline.
  74. - *transports*
  75. ['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling',
  76. 'jsonp-polling']
  77. A list of the accepted transports.
  78. - *transportOptions*
  79. An object of options to pass to each transport. For example `{ websocket: { closeTimeout: 8000 }}`
  80. - *log*
  81. ƒ(){ sys.log }
  82. The logging function. Defaults to outputting to `stdout` through `sys.log`
  83. Events:
  84. - *clientConnect(client)*
  85. Fired when a client is connected. Receives the Client instance as parameter.
  86. - *clientMessage(message, client)*
  87. Fired when a message from a client is received. Receives the message and Client instance as parameters.
  88. - *clientDisconnect(client)*
  89. Fired when a client is disconnected. Receives the Client instance as a parameter.
  90. Important note: `this` in the event listener refers to the `Listener` instance.
  91. ### Client
  92. Client(listener, req, res)
  93. Public Properties:
  94. - *listener*
  95. The `Listener` instance to which this client belongs.
  96. - *connected*
  97. Whether the client is connected.
  98. - *connections*
  99. Number of times the client has connected.
  100. Methods:
  101. - *send(message)*
  102. Sends a message to the client.
  103. - *broadcast(message)*
  104. Sends a message to all other clients. Equivalent to Listener::broadcast(message, client.sessionId).
  105. ## Protocol
  106. One of the design goals is that you should be able to implement whatever protocol you desire without `Socket.IO` getting in the way. `Socket.IO` has a minimal, unobtrusive protocol layer, consisting of two parts:
  107. * Connection handshake
  108. This is required to simulate a full duplex socket with transports such as XHR Polling or Server-sent Events (which is a "one-way socket"). The basic idea is that the first message received from the server will be a JSON object that contains a session ID used for further communications exchanged between the client and server.
  109. The concept of session also naturally benefits a full-duplex WebSocket, in the event of an accidental disconnection and a quick reconnection. Messages that the server intends to deliver to the client are cached temporarily until reconnection.
  110. The implementation of reconnection logic (potentially with retries) is left for the user. By default, transports that are keep-alive or open all the time (like WebSocket) have a timeout of 0 if a disconnection is detected.
  111. * Message batching
  112. Messages are buffered in order to optimize resources. In the event of the server trying to send multiple messages while a client is temporarily disconnected (eg: xhr polling), the messages are stacked and then encoded in a lightweight way, and sent to the client whenever it becomes available.
  113. Despite this extra layer, the messages are delivered unaltered to the various event listeners. You can `JSON.stringify()` objects, send XML, or even plain text.
  114. ## Credits
  115. - Guillermo Rauch &lt;guillermo@learnboost.com&gt; ([Guille](http://github.com/guille))
  116. - Arnout Kazemier ([3rd-Eden](http://github.com/3rd-Eden))
  117. ## License
  118. (The MIT License)
  119. Copyright (c) 2010 LearnBoost &lt;dev@learnboost.com&gt;
  120. Permission is hereby granted, free of charge, to any person obtaining
  121. a copy of this software and associated documentation files (the
  122. 'Software'), to deal in the Software without restriction, including
  123. without limitation the rights to use, copy, modify, merge, publish,
  124. distribute, sublicense, and/or sell copies of the Software, and to
  125. permit persons to whom the Software is furnished to do so, subject to
  126. the following conditions:
  127. The above copyright notice and this permission notice shall be
  128. included in all copies or substantial portions of the Software.
  129. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  130. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  131. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  132. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  133. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  134. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  135. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.