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.

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