WebSocketClient.h 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. // ws://127.0.0.1:8080/
  21. int open(const char* url);
  22. int close();
  23. int send(const std::string& msg);
  24. int send(const char* buf, int len, enum ws_opcode opcode = WS_OPCODE_BINARY);
  25. private:
  26. enum State {
  27. CONNECTING,
  28. CONNECTED,
  29. WS_UPGRADING,
  30. WS_OPENED,
  31. WS_CLOSED,
  32. } state;
  33. HttpParserPtr http_parser_;
  34. HttpRequestPtr http_req_;
  35. HttpResponsePtr http_resp_;
  36. WebSocketParserPtr ws_parser_;
  37. };
  38. }
  39. #endif // HV_WEBSOCKET_CLIENT_H_