1
0

websocket_server_test.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. if (channel->isConnected() && channel->isWriteComplete()) {
  57. char str[DATETIME_FMT_BUFLEN] = {0};
  58. datetime_t dt = datetime_now();
  59. datetime_fmt(&dt, str);
  60. channel->send(str);
  61. }
  62. });
  63. };
  64. ws.onmessage = [](const WebSocketChannelPtr& channel, const std::string& msg) {
  65. MyContext* ctx = channel->getContext<MyContext>();
  66. ctx->handleMessage(msg);
  67. };
  68. ws.onclose = [](const WebSocketChannelPtr& channel) {
  69. printf("onclose\n");
  70. MyContext* ctx = channel->getContext<MyContext>();
  71. if (ctx->timerID != INVALID_TIMER_ID) {
  72. killTimer(ctx->timerID);
  73. }
  74. channel->deleteContext<MyContext>();
  75. };
  76. websocket_server_t server;
  77. server.port = port;
  78. #if TEST_WSS
  79. server.https_port = port + 1;
  80. hssl_ctx_init_param_t param;
  81. memset(&param, 0, sizeof(param));
  82. param.crt_file = "cert/server.crt";
  83. param.key_file = "cert/server.key";
  84. param.endpoint = HSSL_SERVER;
  85. if (hssl_ctx_init(&param) == NULL) {
  86. fprintf(stderr, "hssl_ctx_init failed!\n");
  87. return -20;
  88. }
  89. #endif
  90. server.service = &http;
  91. server.ws = &ws;
  92. websocket_server_run(&server, 0);
  93. // press Enter to stop
  94. while (getchar() != '\n');
  95. websocket_server_stop(&server);
  96. return 0;
  97. }