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.

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