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.

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