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.

283 lines
10 KiB

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