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.

265 lines
9.5 KiB

13 years ago
13 years ago
12 years ago
12 years ago
11 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 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
11 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. Projects using netty-socketio
  26. ================================
  27. ARSnova: [arsnova.eu](https://arsnova.eu)
  28. Zipwhip: [zipwhip.com](https://zipwhip.com/)
  29. Recent Releases
  30. ================================
  31. ####Please Note: trunk is current development branch.
  32. ####06-Mar-2014 - version 1.6.3 released (JDK 1.6+ compatible, Netty 4.0.17)
  33. Fixed - AckCallback handling during client disconnect
  34. Fixed - unauthorized handshake HTTP code changed to 401
  35. __Breaking api change__ - Configuration.heartbeatThreadPoolSize setting removed
  36. Feature - annotated Spring beans support via _SpringAnnotationScanner_
  37. Feature - common exception listener
  38. Improvement - _ScheduledExecutorService_ replaced with _HashedWheelTimer_
  39. ####08-Feb-2014 - version 1.6.2 released (JDK 1.6+ compatible, Netty 4.0.15)
  40. Fixed - wrong namespace client disconnect handling
  41. Fixed - exception in onConnect/onDisconnect/isAuthorized methods leads to server hang
  42. __Breaking api change__ - SocketIOClient.sendEvent methods signature changed
  43. Improvement - multi type events support via _MultiTypeEventListener_ and _OnEvent_ annotation
  44. Improvement - multi type events ack support via _MultiTypeAckCallback_
  45. Improvement - SocketIOClient.getHandshakeData method added
  46. Improvement - Jedis replaced with [Redisson](https://github.com/mrniko/redisson)
  47. ####14-Jan-2014 - version 1.6.1 released (JDK 1.6+ compatible, Netty 4.0.14)
  48. Fixed - JDK 1.6+ compatibility
  49. Feature - authorization support
  50. ####19-Dec-2013 - version 1.6.0 released (JDK 1.6+ compatible, Netty 4.0.13)
  51. Fixed - XHR-pooling transport regression
  52. Fixed - Websocket transport regression
  53. Fixed - namespace NPE in PacketHandler
  54. Fixed - executors shutdown during server stop
  55. Feature - client store (Memory, [Redis](http://redis.io/), [Hazelcast](http://www.hazelcast.com/)) support
  56. Feature - distributed broadcast across netty-socketio nodes ([Redis](http://redis.io/), [Hazelcast](http://www.hazelcast.com/)) support
  57. Feature - OSGi support (thanks to rdevera)
  58. Improvement - XHR-pooling optimization
  59. Improvement - SocketIOClient.getAllRooms method added
  60. ####07-Dec-2013 - version 1.5.4 released (JDK 1.6+ compatible, Netty 4.0.13)
  61. Fixed - flash policy "request leak" after page reload (thanks to ntrp)
  62. Fixed - websocket swf loading (thanks to ntrp)
  63. Fixed - wrong urls causes a potential DDoS
  64. Fixed - Event.class package visibility changed to avoid direct usage
  65. Improvement - Simplified Jackson modules registration
  66. ####24-Oct-2013 - version 1.5.2 released (JDK 1.6+ compatible, Netty 4.0.11)
  67. Fixed - NPE during shutdown
  68. Improvement - isEmpty method added to Namespace
  69. ####13-Oct-2013 - version 1.5.1 released (JDK 1.6+ compatible, Netty 4.0.9)
  70. Fixed - wrong ack timeout callback invocation
  71. Fixed - bigdecimal serialization for JSON
  72. Fixed - infinity loop during packet handling exception
  73. Fixed - 'client not found' handling
  74. ####27-Aug-2013 - version 1.5.0 released (JDK 1.6+ compatible, Netty 4.0.7)
  75. Improvement - encoding buffers allocation optimization.
  76. Improvement - encoding buffers now pooled in memory to reduce GC pressure (netty 4.x feature).
  77. ####03-Aug-2013 - version 1.0.1 released (JDK 1.5+ compatible)
  78. Fixed - error on unknown property during deserialization.
  79. Fixed - memory leak in long polling transport.
  80. Improvement - logging error info with inbound data.
  81. ####07-Jun-2013 - version 1.0.0 released (JDK 1.5+ compatible)
  82. First stable release.
  83. ### Maven
  84. Include the following to your dependency list:
  85. <dependency>
  86. <groupId>com.corundumstudio.socketio</groupId>
  87. <artifactId>netty-socketio</artifactId>
  88. <version>1.6.1</version>
  89. </dependency>
  90. Usage example
  91. ================================
  92. ##Server
  93. Base configuration. More details about Configuration object is [here](https://github.com/mrniko/netty-socketio/wiki/Configuration-details).
  94. Configuration config = new Configuration();
  95. config.setHostname("localhost");
  96. config.setPort(81);
  97. SocketIOServer server = new SocketIOServer(config);
  98. Programmatic handlers binding:
  99. server.addMessageListener(new DataListener<String>() {
  100. @Override
  101. public void onData(SocketIOClient client, String message, AckRequest ackRequest) {
  102. ...
  103. }
  104. });
  105. server.addEventListener("someevent", SomeClass.class, new DataListener<SomeClass>() {
  106. @Override
  107. public void onData(SocketIOClient client, Object data, AckRequest ackRequest) {
  108. ...
  109. }
  110. });
  111. server.addConnectListener(new ConnectListener() {
  112. @Override
  113. public void onConnect(SocketIOClient client) {
  114. ...
  115. }
  116. });
  117. server.addDisconnectListener(new DisconnectListener() {
  118. @Override
  119. public void onDisconnect(SocketIOClient client) {
  120. ...
  121. }
  122. });
  123. // Don't forget to include type field on javascript side,
  124. // it named '@class' by default and should equals to full class name.
  125. //
  126. // TIP: you can customize type field name via Configuration.jsonTypeFieldName property.
  127. server.addJsonObjectListener(SomeClass.class, new DataListener<SomeClass>() {
  128. @Override
  129. public void onData(SocketIOClient client, SomeClass data, AckRequest ackRequest) {
  130. ...
  131. // send object to socket.io client
  132. SampleObject obj = new SampleObject();
  133. client.sendJsonObject(obj);
  134. }
  135. });
  136. Declarative handlers binding. Handlers could be bound via annotations on any object:
  137. pubic class SomeBusinessService {
  138. ...
  139. // some stuff code
  140. ...
  141. // SocketIOClient, AckRequest and Data could be ommited
  142. @OnEvent('someevent')
  143. public void onSomeEventHandler(SocketIOClient client, SomeClass data, AckRequest ackRequest) {
  144. ...
  145. }
  146. @OnConnect
  147. public void onConnectHandler(SocketIOClient client) {
  148. ...
  149. }
  150. @OnDisconnect
  151. public void onDisconnectHandler(SocketIOClient client) {
  152. ...
  153. }
  154. // only data object is required in arguments,
  155. // SocketIOClient and AckRequest could be ommited
  156. @OnJsonObject
  157. public void onSomeEventHandler(SocketIOClient client, SomeClass data, AckRequest ackRequest) {
  158. ...
  159. }
  160. // only data object is required in arguments,
  161. // SocketIOClient and AckRequest could be ommited
  162. @OnMessage
  163. public void onSomeEventHandler(SocketIOClient client, String data, AckRequest ackRequest) {
  164. ...
  165. }
  166. }
  167. SomeBusinessService someService = new SomeBusinessService();
  168. server.addListeners(someService);
  169. server.start();
  170. ...
  171. server.stop();
  172. ##Client
  173. <script type="text/javascript" src="socket.io.js" charset="utf-8"></script>
  174. <script type="text/javascript">
  175. var socket = io.connect('http://localhost:81', {
  176. 'reconnection delay' : 2000,
  177. 'force new connection' : true
  178. });
  179. socket.on('message', function(data) {
  180. // here is your handler on messages from server
  181. });
  182. socket.on('connect', function() {
  183. // connection established, now we can send an objects
  184. // send json-object to server
  185. // '@class' property should be defined and should
  186. // equals to full class name.
  187. var obj = { '@class' : 'com.sample.SomeClass',
  188. ...
  189. };
  190. socket.json.send(obj);
  191. // send event-object to server
  192. // '@class' property is NOT necessary in this case
  193. var event = {
  194. ...
  195. };
  196. socket.emit('someevent', event);
  197. });
  198. </script>