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.

266 lines
9.6 KiB

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