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.

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