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.

320 lines
12 KiB

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