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.

301 lines
12 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
11 years ago
12 years ago
13 years ago
12 years ago
11 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
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__...__0.9.16__ (netty-socketio 1.6.5) and __1.0+__ (netty-socketio latest version) version of [Socket.IO-client](https://github.com/LearnBoost/socket.io-client)
  8. * Supports xhr-polling transport
  9. * Supports flashsocket transport
  10. * Supports websocket transport
  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. Customer feedback in __2012__:
  23. CentOS, 1 CPU, 4GB RAM runned on VM:
  24. CPU 10%, Memory 15%
  25. 6000 xhr-long polling sessions or 15000 websockets sessions
  26. 4000 messages per second
  27. Customer feedback in __2014__:
  28. "To stress test the solution we run 30 000 simultaneous websocket clients and managed to peak at total of about 140 000 messages per second with less than 1 second average delay." (c) Viktor Endersz - Kambi Sports Solutions
  29. Projects using netty-socketio
  30. ================================
  31. AVOS Cloud: [avoscloud.com](https://avoscloud.com/)
  32. Bingo Crack: [bingocrack.com](http://bingocrack.com/)
  33. Kambi Sports Solutions: [kambi.com](http://kambi.com/)
  34. ARSnova: [arsnova.eu](https://arsnova.eu)
  35. Zipwhip: [zipwhip.com](https://zipwhip.com/)
  36. Recent Releases
  37. ================================
  38. ####Please Note: trunk is current development branch.
  39. ####07-Jul-2014 - version 1.7.1 released (SocketIO 1.0+ protocol)
  40. Feature - ability to set custom `Access-Control-Allow-Origin` via Configuration.origin
  41. Fixed - connection via CLI socket.io-client
  42. ####28-Jun-2014 - version 1.7.0 released (SocketIO 1.0+ protocol)
  43. Feature - Socket.IO 1.0 protocol support. Thanks to the new protocol decoding/encoding has speedup
  44. __Dropped__ - `SocketIOClient.sendMessage`, `SocketIOClient.sendJsonObject` methods and corresponding listeners
  45. __Dropped__ - Flashsocket transport support
  46. __Dropped__ - protocol version 0.7 ... 0.9.16
  47. ####13-May-2014 - version 1.6.5 released (JDK 1.6+ compatible)
  48. Improvement - single packet encoding optimized, used mostly in WebSocket transport. Encoding time reduced up to 40% (thanks to Viktor Endersz)
  49. Improvement - rooms handling optimized
  50. Improvement - ExceptionListener.exceptionCaught method added
  51. __Breaking api change__ - Configuration.autoAck replaced with ackMode
  52. Feature - trustStore setting added
  53. Feature - maxFramePayloadLength setting added
  54. Feature - getAllClients and getClient methods added to SocketIONamespace
  55. Fixed - SocketIOServer.getAllClients returns wrong clients amount
  56. ####25-Mar-2014 - version 1.6.4 released (JDK 1.6+ compatible, Netty 4.0.17)
  57. Fixed - message release problem
  58. Fixed - problem with exception listener configuration redefinition
  59. __Breaking api change__ - DataListener.onData now throws Exception
  60. Improvement - data parameter added to exception listener
  61. Improvement - ability to setup socket configuration
  62. Improvement - Configuration.autoAck parameter added
  63. ####06-Mar-2014 - version 1.6.3 released (JDK 1.6+ compatible, Netty 4.0.17)
  64. Fixed - AckCallback handling during client disconnect
  65. Fixed - unauthorized handshake HTTP code changed to 401
  66. __Breaking api change__ - Configuration.heartbeatThreadPoolSize setting removed
  67. Feature - annotated Spring beans support via _SpringAnnotationScanner_
  68. Feature - common exception listener
  69. Improvement - _ScheduledExecutorService_ replaced with _HashedWheelTimer_
  70. ####08-Feb-2014 - version 1.6.2 released (JDK 1.6+ compatible, Netty 4.0.15)
  71. Fixed - wrong namespace client disconnect handling
  72. Fixed - exception in onConnect/onDisconnect/isAuthorized methods leads to server hang
  73. __Breaking api change__ - SocketIOClient.sendEvent methods signature changed
  74. Improvement - multi type events support via _MultiTypeEventListener_ and _OnEvent_ annotation
  75. Improvement - multi type events ack support via _MultiTypeAckCallback_
  76. Improvement - SocketIOClient.getHandshakeData method added
  77. Improvement - Jedis replaced with [Redisson](https://github.com/mrniko/redisson)
  78. ####14-Jan-2014 - version 1.6.1 released (JDK 1.6+ compatible, Netty 4.0.14)
  79. Fixed - JDK 1.6+ compatibility
  80. Feature - authorization support
  81. ####19-Dec-2013 - version 1.6.0 released (JDK 1.6+ compatible, Netty 4.0.13)
  82. Fixed - XHR-pooling transport regression
  83. Fixed - Websocket transport regression
  84. Fixed - namespace NPE in PacketHandler
  85. Fixed - executors shutdown during server stop
  86. Feature - client store (Memory, [Redis](http://redis.io/), [Hazelcast](http://www.hazelcast.com/)) support
  87. Feature - distributed broadcast across netty-socketio nodes ([Redis](http://redis.io/), [Hazelcast](http://www.hazelcast.com/)) support
  88. Feature - OSGi support (thanks to rdevera)
  89. Improvement - XHR-pooling optimization
  90. Improvement - SocketIOClient.getAllRooms method added
  91. ####07-Dec-2013 - version 1.5.4 released (JDK 1.6+ compatible, Netty 4.0.13)
  92. Fixed - flash policy "request leak" after page reload (thanks to ntrp)
  93. Fixed - websocket swf loading (thanks to ntrp)
  94. Fixed - wrong urls causes a potential DDoS
  95. Fixed - Event.class package visibility changed to avoid direct usage
  96. Improvement - Simplified Jackson modules registration
  97. ####24-Oct-2013 - version 1.5.2 released (JDK 1.6+ compatible, Netty 4.0.11)
  98. Fixed - NPE during shutdown
  99. Improvement - isEmpty method added to Namespace
  100. ####13-Oct-2013 - version 1.5.1 released (JDK 1.6+ compatible, Netty 4.0.9)
  101. Fixed - wrong ack timeout callback invocation
  102. Fixed - bigdecimal serialization for JSON
  103. Fixed - infinity loop during packet handling exception
  104. Fixed - 'client not found' handling
  105. ####27-Aug-2013 - version 1.5.0 released (JDK 1.6+ compatible, Netty 4.0.7)
  106. Improvement - encoding buffers allocation optimization.
  107. Improvement - encoding buffers now pooled in memory to reduce GC pressure (netty 4.x feature).
  108. ####03-Aug-2013 - version 1.0.1 released (JDK 1.5+ compatible)
  109. Fixed - error on unknown property during deserialization.
  110. Fixed - memory leak in long polling transport.
  111. Improvement - logging error info with inbound data.
  112. ####07-Jun-2013 - version 1.0.0 released (JDK 1.5+ compatible)
  113. First stable release.
  114. ### Maven
  115. Include the following to your dependency list:
  116. <dependency>
  117. <groupId>com.corundumstudio.socketio</groupId>
  118. <artifactId>netty-socketio</artifactId>
  119. <version>1.6.5</version>
  120. </dependency>
  121. Usage example
  122. ================================
  123. ##Server
  124. Base configuration. More details about Configuration object is [here](https://github.com/mrniko/netty-socketio/wiki/Configuration-details).
  125. Configuration config = new Configuration();
  126. config.setHostname("localhost");
  127. config.setPort(81);
  128. SocketIOServer server = new SocketIOServer(config);
  129. Programmatic handlers binding:
  130. server.addMessageListener(new DataListener<String>() {
  131. @Override
  132. public void onData(SocketIOClient client, String message, AckRequest ackRequest) {
  133. ...
  134. }
  135. });
  136. server.addEventListener("someevent", SomeClass.class, new DataListener<SomeClass>() {
  137. @Override
  138. public void onData(SocketIOClient client, Object data, AckRequest ackRequest) {
  139. ...
  140. }
  141. });
  142. server.addConnectListener(new ConnectListener() {
  143. @Override
  144. public void onConnect(SocketIOClient client) {
  145. ...
  146. }
  147. });
  148. server.addDisconnectListener(new DisconnectListener() {
  149. @Override
  150. public void onDisconnect(SocketIOClient client) {
  151. ...
  152. }
  153. });
  154. // Don't forget to include type field on javascript side,
  155. // it named '@class' by default and should equals to full class name.
  156. //
  157. // TIP: you can customize type field name via Configuration.jsonTypeFieldName property.
  158. server.addJsonObjectListener(SomeClass.class, new DataListener<SomeClass>() {
  159. @Override
  160. public void onData(SocketIOClient client, SomeClass data, AckRequest ackRequest) {
  161. ...
  162. // send object to socket.io client
  163. SampleObject obj = new SampleObject();
  164. client.sendJsonObject(obj);
  165. }
  166. });
  167. Declarative handlers binding. Handlers could be bound via annotations on any object:
  168. pubic class SomeBusinessService {
  169. ...
  170. // some stuff code
  171. ...
  172. // SocketIOClient, AckRequest and Data could be ommited
  173. @OnEvent('someevent')
  174. public void onSomeEventHandler(SocketIOClient client, SomeClass data, AckRequest ackRequest) {
  175. ...
  176. }
  177. @OnConnect
  178. public void onConnectHandler(SocketIOClient client) {
  179. ...
  180. }
  181. @OnDisconnect
  182. public void onDisconnectHandler(SocketIOClient client) {
  183. ...
  184. }
  185. // only data object is required in arguments,
  186. // SocketIOClient and AckRequest could be ommited
  187. @OnJsonObject
  188. public void onSomeEventHandler(SocketIOClient client, SomeClass data, AckRequest ackRequest) {
  189. ...
  190. }
  191. // only data object is required in arguments,
  192. // SocketIOClient and AckRequest could be ommited
  193. @OnMessage
  194. public void onSomeEventHandler(SocketIOClient client, String data, AckRequest ackRequest) {
  195. ...
  196. }
  197. }
  198. SomeBusinessService someService = new SomeBusinessService();
  199. server.addListeners(someService);
  200. server.start();
  201. ...
  202. server.stop();
  203. ##Client
  204. <script type="text/javascript" src="socket.io.js" charset="utf-8"></script>
  205. <script type="text/javascript">
  206. var socket = io.connect('http://localhost:81', {
  207. 'reconnection delay' : 2000,
  208. 'force new connection' : true
  209. });
  210. socket.on('message', function(data) {
  211. // here is your handler on messages from server
  212. });
  213. socket.on('connect', function() {
  214. // connection established, now we can send an objects
  215. // send json-object to server
  216. // '@class' property should be defined and should
  217. // equals to full class name.
  218. var obj = { '@class' : 'com.sample.SomeClass',
  219. ...
  220. };
  221. socket.json.send(obj);
  222. // send event-object to server
  223. // '@class' property is NOT necessary in this case
  224. var event = {
  225. ...
  226. };
  227. socket.emit('someevent', event);
  228. });
  229. </script>