1
0

WebSocketClient.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #ifndef HV_WEBSOCKET_CLIENT_H_
  2. #define HV_WEBSOCKET_CLIENT_H_
  3. /*
  4. * @demo examples/websocket_client_test.cpp
  5. */
  6. #include "hexport.h"
  7. #include "TcpClient.h"
  8. #include "WebSocketChannel.h"
  9. #include "HttpParser.h"
  10. #include "WebSocketParser.h"
  11. namespace hv {
  12. class HV_EXPORT WebSocketClient : public TcpClientTmpl<WebSocketChannel> {
  13. public:
  14. std::string url;
  15. std::function<void()> onopen;
  16. std::function<void()> onclose;
  17. std::function<void(const std::string& msg)> onmessage;
  18. WebSocketClient();
  19. ~WebSocketClient();
  20. // url = ws://ip:port/path
  21. // url = wss://ip:port/path
  22. int open(const char* url);
  23. int close();
  24. int send(const std::string& msg);
  25. int send(const char* buf, int len, enum ws_opcode opcode = WS_OPCODE_BINARY);
  26. private:
  27. enum State {
  28. CONNECTING,
  29. CONNECTED,
  30. WS_UPGRADING,
  31. WS_OPENED,
  32. WS_CLOSED,
  33. } state;
  34. HttpParserPtr http_parser_;
  35. HttpRequestPtr http_req_;
  36. HttpResponsePtr http_resp_;
  37. WebSocketParserPtr ws_parser_;
  38. };
  39. }
  40. #endif // HV_WEBSOCKET_CLIENT_H_