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.

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