netty-demo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
8.6 KiB

13 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
12 years ago
  1. #Netty-socketio Overview
  2. This project is an open-source Java implementation of [Socket.IO](http://socket.io/) server. Based on [Netty](http://netty.io/) server framework.
  3. Checkout [Demo project](https://github.com/mrniko/netty-socketio-demo)
  4. Licensed under the Apache License 2.0.
  5. Features
  6. ================================
  7. * Supports 0.7+ version of [Socket.IO-client](https://github.com/LearnBoost/socket.io-client) up to latest
  8. * Supports xhr-polling transport
  9. * Supports flashsocket transport
  10. * Supports websocket transport: HyBi 00 (which is the same as Hixie 76), HyBi 8-10 and HyBi 13-17 (17 is the same as IETF 6455).
  11. * Supports namespaces and rooms
  12. * Supports ack (acknowledgment of received data)
  13. * Supports SSL
  14. * Supports client store (Memory, [Redisson](https://github.com/mrniko/redisson), [Hazelcast](http://www.hazelcast.com/))
  15. * Supports distributed broadcast across netty-socketio nodes ([Redisson](https://github.com/mrniko/redisson), [Hazelcast](http://www.hazelcast.com/))
  16. * Supports OSGi
  17. * Lock-free and thread-safe implementation
  18. * Declarative handler configuration via annotations
  19. Performance
  20. ================================
  21. CentOS, 1 CPU, 4GB RAM runned on VM (based on customer report, tested in 2012):
  22. CPU 10%, Memory 15%
  23. 6000 xhr-long polling sessions or 15000 websockets sessions
  24. 4000 messages per second
  25. Recent Releases
  26. ================================
  27. ####Please Note: trunk is current development branch.
  28. ####??-Jan-2014 - version 1.6.2 released (JDK 1.6+ compatible, Netty 4.0.14)
  29. Fixed - exception in onConnect/onDisconnect/isAuthorized methods leads to server hang
  30. Improvement - SocketIOClient.getHandshakeData method added
  31. Improvement - Jedis replaced with [Redisson](https://github.com/mrniko/redisson)
  32. ####14-Jan-2014 - version 1.6.1 released (JDK 1.6+ compatible, Netty 4.0.14)
  33. Fixed - JDK 1.6+ compatibility
  34. Feature - authorization support
  35. ####19-Dec-2013 - version 1.6.0 released (JDK 1.6+ compatible, Netty 4.0.13)
  36. Fixed - XHR-pooling transport regression
  37. Fixed - Websocket transport regression
  38. Fixed - namespace NPE in PacketHandler
  39. Fixed - executors shutdown during server stop
  40. Feature - client store (Memory, [Redis](http://redis.io/), [Hazelcast](http://www.hazelcast.com/)) support
  41. Feature - distributed broadcast across netty-socketio nodes ([Redis](http://redis.io/), [Hazelcast](http://www.hazelcast.com/)) support
  42. Feature - OSGi support (thanks to rdevera)
  43. Improvement - XHR-pooling optimization
  44. Improvement - SocketIOClient.getAllRooms method added
  45. ####07-Dec-2013 - version 1.5.4 released (JDK 1.6+ compatible, Netty 4.0.13)
  46. Fixed - flash policy "request leak" after page reload (thanks to ntrp)
  47. Fixed - websocket swf loading (thanks to ntrp)
  48. Fixed - wrong urls causes a potential DDoS
  49. Fixed - Event.class package visibility changed to avoid direct usage
  50. Improvement - Simplified Jackson modules registration
  51. ####24-Oct-2013 - version 1.5.2 released (JDK 1.6+ compatible, Netty 4.0.11)
  52. Fixed - NPE during shutdown
  53. Improvement - isEmpty method added to Namespace
  54. ####13-Oct-2013 - version 1.5.1 released (JDK 1.6+ compatible, Netty 4.0.9)
  55. Fixed - wrong ack timeout callback invocation
  56. Fixed - bigdecimal serialization for JSON
  57. Fixed - infinity loop during packet handling exception
  58. Fixed - 'client not found' handling
  59. ####27-Aug-2013 - version 1.5.0 released (JDK 1.6+ compatible, Netty 4.0.7)
  60. Improvement - encoding buffers allocation optimization.
  61. Improvement - encoding buffers now pooled in memory to reduce GC pressure (netty 4.x feature).
  62. ####03-Aug-2013 - version 1.0.1 released (JDK 1.5+ compatible)
  63. Fixed - error on unknown property during deserialization.
  64. Fixed - memory leak in long polling transport.
  65. Improvement - logging error info with inbound data.
  66. ####07-Jun-2013 - version 1.0.0 released (JDK 1.5+ compatible)
  67. First stable release.
  68. ### Maven
  69. Include the following to your dependency list:
  70. <dependency>
  71. <groupId>com.corundumstudio.socketio</groupId>
  72. <artifactId>netty-socketio</artifactId>
  73. <version>1.6.1</version>
  74. </dependency>
  75. Usage example
  76. ================================
  77. ##Server
  78. Base configuration. More details about Configuration object is [here](https://github.com/mrniko/netty-socketio/wiki/Configuration-details).
  79. Configuration config = new Configuration();
  80. config.setHostname("localhost");
  81. config.setPort(81);
  82. SocketIOServer server = new SocketIOServer(config);
  83. Programmatic handlers binding:
  84. server.addMessageListener(new DataListener<String>() {
  85. @Override
  86. public void onData(SocketIOClient client, String message, AckRequest ackRequest) {
  87. ...
  88. }
  89. });
  90. server.addEventListener("someevent", SomeClass.class, new DataListener<SomeClass>() {
  91. @Override
  92. public void onData(SocketIOClient client, Object data, AckRequest ackRequest) {
  93. ...
  94. }
  95. });
  96. server.addConnectListener(new ConnectListener() {
  97. @Override
  98. public void onConnect(SocketIOClient client) {
  99. ...
  100. }
  101. });
  102. server.addDisconnectListener(new DisconnectListener() {
  103. @Override
  104. public void onDisconnect(SocketIOClient client) {
  105. ...
  106. }
  107. });
  108. // Don't forget to include type field on javascript side,
  109. // it named '@class' by default and should equals to full class name.
  110. //
  111. // TIP: you can customize type field name via Configuration.jsonTypeFieldName property.
  112. server.addJsonObjectListener(SomeClass.class, new DataListener<SomeClass>() {
  113. @Override
  114. public void onData(SocketIOClient client, SomeClass data, AckRequest ackRequest) {
  115. ...
  116. // send object to socket.io client
  117. SampleObject obj = new SampleObject();
  118. client.sendJsonObject(obj);
  119. }
  120. });
  121. Declarative handlers binding. Handlers could be bound via annotations on any object:
  122. pubic class SomeBusinessService {
  123. ...
  124. // some stuff code
  125. ...
  126. // SocketIOClient, AckRequest and Data could be ommited
  127. @OnEvent('someevent')
  128. public void onSomeEventHandler(SocketIOClient client, SomeClass data, AckRequest ackRequest) {
  129. ...
  130. }
  131. @OnConnect
  132. public void onConnectHandler(SocketIOClient client) {
  133. ...
  134. }
  135. @OnDisconnect
  136. public void onDisconnectHandler(SocketIOClient client) {
  137. ...
  138. }
  139. // only data object is required in arguments,
  140. // SocketIOClient and AckRequest could be ommited
  141. @OnJsonObject
  142. public void onSomeEventHandler(SocketIOClient client, SomeClass data, AckRequest ackRequest) {
  143. ...
  144. }
  145. // only data object is required in arguments,
  146. // SocketIOClient and AckRequest could be ommited
  147. @OnMessage
  148. public void onSomeEventHandler(SocketIOClient client, String data, AckRequest ackRequest) {
  149. ...
  150. }
  151. }
  152. SomeBusinessService someService = new SomeBusinessService();
  153. server.addListeners(someService);
  154. server.start();
  155. ...
  156. server.stop();
  157. ##Client
  158. <script type="text/javascript" src="socket.io.js" charset="utf-8"></script>
  159. <script type="text/javascript">
  160. var socket = io.connect('http://localhost:81', {
  161. 'reconnection delay' : 2000,
  162. 'force new connection' : true
  163. });
  164. socket.on('message', function(data) {
  165. // here is your handler on messages from server
  166. });
  167. socket.on('connect', function() {
  168. // connection established, now we can send an objects
  169. // send json-object to server
  170. // '@class' property should be defined and should
  171. // equals to full class name.
  172. var obj = { '@class' : 'com.sample.SomeClass',
  173. ...
  174. };
  175. socket.json.send(obj);
  176. // send event-object to server
  177. // '@class' property is NOT necessary in this case
  178. var event = {
  179. ...
  180. };
  181. socket.emit('someevent', event);
  182. });
  183. </script>