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.

274 lines
10 KiB

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