websocket_server_test.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. * @python scripts/websocket_server.py
  8. * @js html/websocket_client.html
  9. *
  10. */
  11. #include "WebSocketServer.h"
  12. #include "EventLoop.h"
  13. #include "htime.h"
  14. using namespace hv;
  15. /*
  16. * #define TEST_WSS 1
  17. *
  18. * @build ./configure --with-openssl && make clean && make
  19. *
  20. * @server bin/websocket_server_test 9999
  21. *
  22. * @client bin/websocket_client_test ws://127.0.0.1:9999/
  23. * bin/websocket_client_test wss://127.0.0.1:10000/
  24. *
  25. */
  26. #define TEST_WSS 0
  27. using namespace hv;
  28. class MyContext {
  29. public:
  30. MyContext() {
  31. printf("MyContext::MyContext()\n");
  32. timerID = INVALID_TIMER_ID;
  33. }
  34. ~MyContext() {
  35. printf("MyContext::~MyContext()\n");
  36. }
  37. int handleMessage(const std::string& msg, enum ws_opcode opcode) {
  38. printf("onmessage(type=%s len=%d): %.*s\n", opcode == WS_OPCODE_TEXT ? "text" : "binary",
  39. (int)msg.size(), (int)msg.size(), msg.data());
  40. return msg.size();
  41. }
  42. TimerID timerID;
  43. };
  44. int main(int argc, char** argv) {
  45. if (argc < 2) {
  46. printf("Usage: %s port\n", argv[0]);
  47. return -10;
  48. }
  49. int port = atoi(argv[1]);
  50. HttpService http;
  51. http.GET("/ping", [](const HttpContextPtr& ctx) {
  52. return ctx->send("pong");
  53. });
  54. WebSocketService ws;
  55. // ws.setPingInterval(10000);
  56. ws.onopen = [](const WebSocketChannelPtr& channel, const HttpRequestPtr& req) {
  57. printf("onopen: GET %s\n", req->Path().c_str());
  58. auto ctx = channel->newContextPtr<MyContext>();
  59. // send(time) every 1s
  60. ctx->timerID = setInterval(1000, [channel](TimerID id) {
  61. if (channel->isConnected() && channel->isWriteComplete()) {
  62. char str[DATETIME_FMT_BUFLEN] = {0};
  63. datetime_t dt = datetime_now();
  64. datetime_fmt(&dt, str);
  65. channel->send(str);
  66. }
  67. });
  68. };
  69. ws.onmessage = [](const WebSocketChannelPtr& channel, const std::string& msg) {
  70. auto ctx = channel->getContextPtr<MyContext>();
  71. ctx->handleMessage(msg, channel->opcode);
  72. };
  73. ws.onclose = [](const WebSocketChannelPtr& channel) {
  74. printf("onclose\n");
  75. auto ctx = channel->getContextPtr<MyContext>();
  76. if (ctx->timerID != INVALID_TIMER_ID) {
  77. killTimer(ctx->timerID);
  78. ctx->timerID = INVALID_TIMER_ID;
  79. }
  80. // channel->deleteContextPtr();
  81. };
  82. WebSocketServer server;
  83. server.port = port;
  84. #if TEST_WSS
  85. server.https_port = port + 1;
  86. hssl_ctx_opt_t param;
  87. memset(&param, 0, sizeof(param));
  88. param.crt_file = "cert/server.crt";
  89. param.key_file = "cert/server.key";
  90. param.endpoint = HSSL_SERVER;
  91. if (server.newSslCtx(&param) != 0) {
  92. fprintf(stderr, "new SSL_CTX failed!\n");
  93. return -20;
  94. }
  95. #endif
  96. server.registerHttpService(&http);
  97. server.registerWebSocketService(&ws);
  98. server.start();
  99. // press Enter to stop
  100. while (getchar() != '\n');
  101. return 0;
  102. }