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.

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