1
0

websocket_server_test.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * websocket server
  3. *
  4. * @build make examples
  5. * @server bin/websocket_server_test 9999
  6. * @client bin/websocket_client_test ws://127.0.0.1:9999/
  7. * @js html/websocket_client.html
  8. *
  9. */
  10. #include "WebSocketServer.h"
  11. #include "EventLoop.h"
  12. #include "htime.h"
  13. #include "hssl.h"
  14. /*
  15. * #define TEST_WSS 1
  16. *
  17. * @build ./configure --with-openssl && make clean && make
  18. *
  19. * @server bin/websocket_server_test 9999
  20. *
  21. * @client bin/websocket_client_test ws://127.0.0.1:9999/
  22. * bin/websocket_client_test wss://127.0.0.1:10000/
  23. *
  24. */
  25. #define TEST_WSS 0
  26. using namespace hv;
  27. class MyContext {
  28. public:
  29. MyContext() {
  30. timerID = INVALID_TIMER_ID;
  31. }
  32. ~MyContext() {
  33. }
  34. int handleMessage(const std::string& msg) {
  35. printf("onmessage: %s\n", msg.c_str());
  36. return msg.size();
  37. }
  38. TimerID timerID;
  39. };
  40. int main(int argc, char** argv) {
  41. if (argc < 2) {
  42. printf("Usage: %s port\n", argv[0]);
  43. return -10;
  44. }
  45. int port = atoi(argv[1]);
  46. HttpService http;
  47. http.GET("/ping", [](const HttpContextPtr& ctx) {
  48. return ctx->send("pong");
  49. });
  50. WebSocketService ws;
  51. ws.onopen = [](const WebSocketChannelPtr& channel, const std::string& url) {
  52. printf("onopen: GET %s\n", url.c_str());
  53. MyContext* ctx = channel->newContext<MyContext>();
  54. // send(time) every 1s
  55. ctx->timerID = setInterval(1000, [channel](TimerID id) {
  56. char str[DATETIME_FMT_BUFLEN] = {0};
  57. datetime_t dt = datetime_now();
  58. datetime_fmt(&dt, str);
  59. channel->send(str);
  60. });
  61. };
  62. ws.onmessage = [](const WebSocketChannelPtr& channel, const std::string& msg) {
  63. MyContext* ctx = channel->getContext<MyContext>();
  64. ctx->handleMessage(msg);
  65. };
  66. ws.onclose = [](const WebSocketChannelPtr& channel) {
  67. printf("onclose\n");
  68. MyContext* ctx = channel->getContext<MyContext>();
  69. if (ctx->timerID != INVALID_TIMER_ID) {
  70. killTimer(ctx->timerID);
  71. }
  72. channel->deleteContext<MyContext>();
  73. };
  74. websocket_server_t server;
  75. server.port = port;
  76. #if TEST_WSS
  77. server.https_port = port + 1;
  78. hssl_ctx_init_param_t param;
  79. memset(&param, 0, sizeof(param));
  80. param.crt_file = "cert/server.crt";
  81. param.key_file = "cert/server.key";
  82. param.endpoint = HSSL_SERVER;
  83. if (hssl_ctx_init(&param) == NULL) {
  84. fprintf(stderr, "hssl_ctx_init failed!\n");
  85. return -20;
  86. }
  87. #endif
  88. server.service = &http;
  89. server.ws = &ws;
  90. websocket_server_run(&server, 0);
  91. // press Enter to stop
  92. while (getchar() != '\n');
  93. websocket_server_stop(&server);
  94. return 0;
  95. }