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.

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