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.

224 lines
7.3 KiB

13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
12 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+ version of [Socket.IO-client](https://github.com/LearnBoost/socket.io-client) up to latest - 0.9.11
  8. * Supports xhr-polling transport
  9. * Supports flashsocket transport
  10. * Supports websocket transport: HyBi 00 (which is the same as Hixie 76), HyBi 8-10 and HyBi 13-17 (17 is the same as IETF 6455).
  11. * Supports namespaces
  12. * Supports ack (acknowledgment of received data)
  13. * Supports SSL
  14. * Supports Rooms
  15. * Lock-free implementation
  16. * Declarative handler configuration via annotations
  17. Performance
  18. ================================
  19. CentOS, 1 CPU, 4GB RAM runned on VM (based on customer report, tested in 2012):
  20. CPU 10%, Memory 15% - 6000 xhr-long polling sessions, 15000 websockets sessions, 4000 messages per sec
  21. Recent Releases
  22. ================================
  23. ####Please Note: trunk is current development branch.
  24. ####07-Dec-2013 - version 1.5.4 released (JDK 1.6+ compatible, Netty 4.0.13)
  25. Fixed - flash policy "request leak" after page reload (thanks to ntrp)
  26. Fixed - websocket swf loading (thanks to ntrp)
  27. Fixed - wrong urls causes a potential DDoS
  28. Fixed - Event.class package visibility changed to avoid direct usage
  29. Improvement - Simplified Jackson modules registration
  30. ####24-Oct-2013 - version 1.5.2 released (JDK 1.6+ compatible, Netty 4.0.11)
  31. Fixed - NPE during shutdown
  32. Improvement - isEmpty method added to Namespace
  33. ####13-Oct-2013 - version 1.5.1 released (JDK 1.6+ compatible, Netty 4.0.9)
  34. Fixed - wrong ack timeout callback invocation
  35. Fixed - bigdecimal serialization for JSON
  36. Fixed - infinity loop during packet handling exception
  37. Fixed - 'client not found' handling
  38. ####27-Aug-2013 - version 1.5.0 released (JDK 1.6+ compatible, Netty 4.0.7)
  39. Improvement - encoding buffers allocation optimization.
  40. Improvement - encoding buffers now pooled in memory to reduce GC pressure (netty 4.x feature).
  41. ####03-Aug-2013 - version 1.0.1 released (JDK 1.5+ compatible)
  42. Fixed - error on unknown property during deserialization.
  43. Fixed - memory leak in long polling transport.
  44. Improvement - logging error info with inbound data.
  45. ####07-Jun-2013 - version 1.0.0 released (JDK 1.5+ compatible)
  46. First stable release.
  47. ### Maven
  48. Include the following to your dependency list:
  49. <dependency>
  50. <groupId>com.corundumstudio.socketio</groupId>
  51. <artifactId>netty-socketio</artifactId>
  52. <version>1.5.4</version>
  53. </dependency>
  54. Usage example
  55. ================================
  56. ##Server
  57. Base configuration. More details about Configuration object is [here](https://github.com/mrniko/netty-socketio/wiki/Configuration-details).
  58. Configuration config = new Configuration();
  59. config.setHostname("localhost");
  60. config.setPort(81);
  61. SocketIOServer server = new SocketIOServer(config);
  62. Programmatic handlers binding:
  63. server.addMessageListener(new DataListener<String>() {
  64. @Override
  65. public void onData(SocketIOClient client, String message, AckRequest ackRequest) {
  66. ...
  67. }
  68. });
  69. server.addEventListener("someevent", SomeClass.class, new DataListener<SomeClass>() {
  70. @Override
  71. public void onData(SocketIOClient client, Object data, AckRequest ackRequest) {
  72. ...
  73. }
  74. });
  75. server.addConnectListener(new ConnectListener() {
  76. @Override
  77. public void onConnect(SocketIOClient client) {
  78. ...
  79. }
  80. });
  81. server.addDisconnectListener(new DisconnectListener() {
  82. @Override
  83. public void onDisconnect(SocketIOClient client) {
  84. ...
  85. }
  86. });
  87. // Don't forget to include type field on javascript side,
  88. // it named '@class' by default and should equals to full class name.
  89. //
  90. // TIP: you can customize type field name via Configuration.jsonTypeFieldName property.
  91. server.addJsonObjectListener(SomeClass.class, new DataListener<SomeClass>() {
  92. @Override
  93. public void onData(SocketIOClient client, SomeClass data, AckRequest ackRequest) {
  94. ...
  95. // send object to socket.io client
  96. SampleObject obj = new SampleObject();
  97. client.sendJsonObject(obj);
  98. }
  99. });
  100. Declarative handlers binding. Handlers could be bound via annotations on any object:
  101. pubic class SomeBusinessService {
  102. ...
  103. // some stuff code
  104. ...
  105. // SocketIOClient, AckRequest and Data could be ommited
  106. @OnEvent('someevent')
  107. public void onSomeEventHandler(SocketIOClient client, SomeClass data, AckRequest ackRequest) {
  108. ...
  109. }
  110. @OnConnect
  111. public void onConnectHandler(SocketIOClient client) {
  112. ...
  113. }
  114. @OnDisconnect
  115. public void onDisconnectHandler(SocketIOClient client) {
  116. ...
  117. }
  118. // only data object is required in arguments,
  119. // SocketIOClient and AckRequest could be ommited
  120. @OnJsonObject
  121. public void onSomeEventHandler(SocketIOClient client, SomeClass data, AckRequest ackRequest) {
  122. ...
  123. }
  124. // only data object is required in arguments,
  125. // SocketIOClient and AckRequest could be ommited
  126. @OnMessage
  127. public void onSomeEventHandler(SocketIOClient client, String data, AckRequest ackRequest) {
  128. ...
  129. }
  130. }
  131. SomeBusinessService someService = new SomeBusinessService();
  132. server.addListeners(someService);
  133. server.start();
  134. ...
  135. server.stop();
  136. ##Client
  137. <script type="text/javascript" src="socket.io.js" charset="utf-8"></script>
  138. <script type="text/javascript">
  139. var socket = io.connect('http://localhost:81', {
  140. 'reconnection delay' : 2000,
  141. 'force new connection' : true
  142. });
  143. socket.on('message', function(data) {
  144. // here is your handler on messages from server
  145. });
  146. socket.on('connect', function() {
  147. // connection established, now we can send an objects
  148. // send json-object to server
  149. // '@class' property should be defined and should
  150. // equals to full class name.
  151. var obj = { '@class' : 'com.sample.SomeClass',
  152. ...
  153. };
  154. socket.json.send(obj);
  155. // send event-object to server
  156. // '@class' property is NOT necessary in this case
  157. var event = {
  158. ...
  159. };
  160. socket.emit('someevent', event);
  161. });
  162. </script>