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.

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