websocket_server_test.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. using namespace hv;
  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. printf("MyContext::MyContext()\n");
  31. timerID = INVALID_TIMER_ID;
  32. }
  33. ~MyContext() {
  34. printf("MyContext::~MyContext()\n");
  35. }
  36. int handleMessage(const std::string& msg, enum ws_opcode opcode) {
  37. printf("onmessage(type=%s len=%d): %.*s\n", opcode == WS_OPCODE_TEXT ? "text" : "binary",
  38. (int)msg.size(), (int)msg.size(), msg.data());
  39. return msg.size();
  40. }
  41. TimerID timerID;
  42. };
  43. int main(int argc, char** argv) {
  44. if (argc < 2) {
  45. printf("Usage: %s port\n", argv[0]);
  46. return -10;
  47. }
  48. int port = atoi(argv[1]);
  49. HttpService http;
  50. http.GET("/ping", [](const HttpContextPtr& ctx) {
  51. return ctx->send("pong");
  52. });
  53. WebSocketService ws;
  54. ws.onopen = [](const WebSocketChannelPtr& channel, const HttpRequestPtr& req) {
  55. printf("onopen: GET %s\n", req->Path().c_str());
  56. auto ctx = channel->newContextPtr<MyContext>();
  57. // send(time) every 1s
  58. ctx->timerID = setInterval(1000, [channel](TimerID id) {
  59. if (channel->isConnected() && channel->isWriteComplete()) {
  60. char str[DATETIME_FMT_BUFLEN] = {0};
  61. datetime_t dt = datetime_now();
  62. datetime_fmt(&dt, str);
  63. channel->send(str);
  64. }
  65. });
  66. };
  67. ws.onmessage = [](const WebSocketChannelPtr& channel, const std::string& msg) {
  68. auto ctx = channel->getContextPtr<MyContext>();
  69. ctx->handleMessage(msg, channel->opcode);
  70. };
  71. ws.onclose = [](const WebSocketChannelPtr& channel) {
  72. printf("onclose\n");
  73. auto ctx = channel->getContextPtr<MyContext>();
  74. if (ctx->timerID != INVALID_TIMER_ID) {
  75. killTimer(ctx->timerID);
  76. ctx->timerID = INVALID_TIMER_ID;
  77. }
  78. // channel->deleteContextPtr();
  79. };
  80. WebSocketServer server;
  81. server.port = port;
  82. #if TEST_WSS
  83. server.https_port = port + 1;
  84. hssl_ctx_opt_t param;
  85. memset(&param, 0, sizeof(param));
  86. param.crt_file = "cert/server.crt";
  87. param.key_file = "cert/server.key";
  88. param.endpoint = HSSL_SERVER;
  89. if (server.newSslCtx(&param) != 0) {
  90. fprintf(stderr, "new SSL_CTX failed!\n");
  91. return -20;
  92. }
  93. #endif
  94. server.registerHttpService(&http);
  95. server.registerWebSocketService(&ws);
  96. server.start();
  97. // press Enter to stop
  98. while (getchar() != '\n');
  99. return 0;
  100. }