WebSocketClient.h 986 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. private:
  25. enum State {
  26. CONNECTING,
  27. CONNECTED,
  28. WS_UPGRADING,
  29. WS_OPENED,
  30. WS_CLOSED,
  31. } state;
  32. HttpParserPtr http_parser;
  33. HttpRequestPtr http_req;
  34. HttpResponsePtr http_resp;
  35. WebSocketParserPtr ws_parser;
  36. };
  37. }
  38. #endif // HV_WEBSOCKET_CLIENT_H_