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.

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