1
0

websocket_server_test.cpp 2.9 KB

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