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.

87 lines
2.2 KiB

13 years ago
13 years ago
13 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
13 years ago
  1. #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. Licensed under the Apache License 2.0.
  5. ### Features
  6. * Supports 0.7+ version of [Socket.IO-client](https://github.com/LearnBoost/socket.io-client) up to latest - 0.9.6
  7. * Supports xhr-polling transport
  8. * Supports websocket transport (Hixie-75/76/Hybi-00, Hybi-10..Hybi-13)
  9. #Usage example
  10. ##Server
  11. SocketIOListener handler = new SocketIOListener() {
  12. @Override
  13. public void onEvent(SocketIOClient client, Packet packet) {
  14. ...
  15. }
  16. @Override
  17. public void onMessage(SocketIOClient client, Packet packet) {
  18. // get a message
  19. packet.getData().toString();
  20. ...
  21. }
  22. @Override
  23. public void onDisconnect(SocketIOClient client) {
  24. ...
  25. }
  26. @Override
  27. public void onConnect(final SocketIOClient client) {
  28. ...
  29. }
  30. @Override
  31. public void onJsonObject(SocketIOClient client, Packet packet) {
  32. // get a json object
  33. packet.getData();
  34. ...
  35. SampleObject obj = new SampleObject();
  36. // send object to socket.io client
  37. client.sendJsonObject(obj);
  38. }
  39. };
  40. Configuration config = new Configuration();
  41. config.setHostname("localhost");
  42. config.setPort(81);
  43. config.setListener(handler);
  44. SocketIOServer server = new SocketIOServer(config);
  45. server.start();
  46. ...
  47. server.stop();
  48. ##Client
  49. <script type="text/javascript" src="socket.io.js" charset="utf-8"></script>
  50. <script type="text/javascript">
  51. var socket = io.connect('http://localhost:81', {
  52. 'reconnection delay' : 2000,
  53. 'force new connection' : true
  54. });
  55. socket.on('message', function(data) {
  56. // here is your handler on messages from server
  57. });
  58. socket.on('connect', function() {
  59. // connection established, now we can send an objects
  60. // send json-object to server
  61. var obj = ...
  62. socket.json.send(obj);
  63. });
  64. </script>