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.

205 lines
6.5 KiB

13 years ago
13 years ago
13 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
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 (Hixie-75/76/Hybi-00, Hybi-10..Hybi-13)
  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. Recent Releases
  18. ================================
  19. ####Please Note: trunk is current development branch.
  20. ####13-Oct-2013 - version 1.5.1 released (JDK 1.6+ compatible, Netty 4.0.9)
  21. Fixed - wrong ack timeout invocation
  22. Fixed - bigdecimal serialization for JSON
  23. Fixed - infinity loop during packet handling exception
  24. Fixed - 'client not found' handling
  25. ####27-Aug-2013 - version 1.5.0 released (JDK 1.6+ compatible, Netty 4.0.7)
  26. Improvement - encoding buffers allocation optimization.
  27. Improvement - encoding buffers now pooled in memory to reduce GC pressure (netty 4.x feature).
  28. ####03-Aug-2013 - version 1.0.1 released (JDK 1.5+ compatible)
  29. Fixed - error on unknown property during deserialization.
  30. Fixed - memory leak in long polling transport.
  31. Improvement - logging error info with inbound data.
  32. ####07-Jun-2013 - version 1.0.0 released (JDK 1.5+ compatible)
  33. First stable release.
  34. ### Maven
  35. Include the following to your dependency list:
  36. <dependency>
  37. <groupId>com.corundumstudio.socketio</groupId>
  38. <artifactId>netty-socketio</artifactId>
  39. <version>1.5.0</version>
  40. </dependency>
  41. Usage example
  42. ================================
  43. ##Server
  44. Base configuration. More details about Configuration object is [here](https://github.com/mrniko/netty-socketio/wiki/Configuration-details).
  45. Configuration config = new Configuration();
  46. config.setHostname("localhost");
  47. config.setPort(81);
  48. SocketIOServer server = new SocketIOServer(config);
  49. Programmatic handlers binding:
  50. server.addMessageListener(new DataListener<String>() {
  51. @Override
  52. public void onData(SocketIOClient client, String message, AckRequest ackRequest) {
  53. ...
  54. }
  55. });
  56. server.addEventListener("someevent", SomeClass.class, new DataListener<SomeClass>() {
  57. @Override
  58. public void onData(SocketIOClient client, Object data, AckRequest ackRequest) {
  59. ...
  60. }
  61. });
  62. server.addConnectListener(new ConnectListener() {
  63. @Override
  64. public void onConnect(SocketIOClient client) {
  65. ...
  66. }
  67. });
  68. server.addDisconnectListener(new DisconnectListener() {
  69. @Override
  70. public void onDisconnect(SocketIOClient client) {
  71. ...
  72. }
  73. });
  74. // Don't forget to include type field on javascript side,
  75. // it named '@class' by default and should equals to full class name.
  76. //
  77. // TIP: you can customize type field name via Configuration.jsonTypeFieldName property.
  78. server.addJsonObjectListener(SomeClass.class, new DataListener<SomeClass>() {
  79. @Override
  80. public void onData(SocketIOClient client, SomeClass data, AckRequest ackRequest) {
  81. ...
  82. // send object to socket.io client
  83. SampleObject obj = new SampleObject();
  84. client.sendJsonObject(obj);
  85. }
  86. });
  87. Declarative handlers binding. Handlers could be bound via annotations on any object:
  88. pubic class SomeBusinessService {
  89. ...
  90. // some stuff code
  91. ...
  92. // SocketIOClient, AckRequest and Data could be ommited
  93. @OnEvent('someevent')
  94. public void onSomeEventHandler(SocketIOClient client, SomeClass data, AckRequest ackRequest) {
  95. ...
  96. }
  97. @OnConnect
  98. public void onConnectHandler(SocketIOClient client) {
  99. ...
  100. }
  101. @OnDisconnect
  102. public void onDisconnectHandler(SocketIOClient client) {
  103. ...
  104. }
  105. // only data object is required in arguments,
  106. // SocketIOClient and AckRequest could be ommited
  107. @OnJsonObject
  108. public void onSomeEventHandler(SocketIOClient client, SomeClass data, AckRequest ackRequest) {
  109. ...
  110. }
  111. // only data object is required in arguments,
  112. // SocketIOClient and AckRequest could be ommited
  113. @OnMessage
  114. public void onSomeEventHandler(SocketIOClient client, String data, AckRequest ackRequest) {
  115. ...
  116. }
  117. }
  118. SomeBusinessService someService = new SomeBusinessService();
  119. server.addListeners(someService);
  120. server.start();
  121. ...
  122. server.stop();
  123. ##Client
  124. <script type="text/javascript" src="socket.io.js" charset="utf-8"></script>
  125. <script type="text/javascript">
  126. var socket = io.connect('http://localhost:81', {
  127. 'reconnection delay' : 2000,
  128. 'force new connection' : true
  129. });
  130. socket.on('message', function(data) {
  131. // here is your handler on messages from server
  132. });
  133. socket.on('connect', function() {
  134. // connection established, now we can send an objects
  135. // send json-object to server
  136. // '@class' property should be defined and should
  137. // equals to full class name.
  138. var obj = { '@class' : 'com.sample.SomeClass',
  139. ...
  140. };
  141. socket.json.send(obj);
  142. // send event-object to server
  143. // '@class' property is NOT necessary in this case
  144. var event = {
  145. ...
  146. };
  147. socket.emit('someevent', event);
  148. });
  149. </script>