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.

257 lines
9.2 KiB

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