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.

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