Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

90 wiersze
3.1 KiB

  1. #ifndef EASYWSCLIENT_HPP_20120819_MIOFVASDTNUASZDQPLFD
  2. #define EASYWSCLIENT_HPP_20120819_MIOFVASDTNUASZDQPLFD
  3. // This code comes from:
  4. // https://github.com/dhbaird/easywsclient
  5. //
  6. // To get the latest version:
  7. // wget https://raw.github.com/dhbaird/easywsclient/master/easywsclient.hpp
  8. // wget https://raw.github.com/dhbaird/easywsclient/master/easywsclient.cpp
  9. #include <string>
  10. #include <vector>
  11. namespace easywsclient
  12. {
  13. struct Callback_Imp
  14. {
  15. virtual void operator()(const std::string &message) = 0;
  16. };
  17. struct BytesCallback_Imp
  18. {
  19. virtual void operator()(const std::vector<uint8_t> &message) = 0;
  20. };
  21. class WebSocket
  22. {
  23. public:
  24. typedef WebSocket *pointer;
  25. typedef enum readyStateValues
  26. {
  27. CLOSING,
  28. CLOSED,
  29. CONNECTING,
  30. OPEN
  31. } readyStateValues;
  32. // Factories:
  33. static pointer create_dummy();
  34. static pointer from_url(const std::string &url, const std::string &origin = std::string());
  35. static pointer from_url(const std::string &url, const std::string &origin = std::string(), bool internal = false);
  36. static pointer from_url_no_mask(const std::string &url, const std::string &origin = std::string());
  37. // Interfaces:
  38. virtual ~WebSocket() {}
  39. virtual void poll(int timeout = 0) = 0; // timeout in milliseconds
  40. virtual void send(const std::string &message) = 0;
  41. virtual void sendBinary(const std::string &message) = 0;
  42. virtual void sendBinary(const std::vector<uint8_t> &message) = 0;
  43. virtual void sendPing() = 0;
  44. virtual void close() = 0;
  45. virtual readyStateValues getReadyState() const = 0;
  46. template <class Callable>
  47. void dispatch(Callable callable)
  48. // For callbacks that accept a string argument.
  49. { // N.B. this is compatible with both C++11 lambdas, functors and C function pointers
  50. struct _Callback : public Callback_Imp
  51. {
  52. Callable &callable;
  53. _Callback(Callable &callable) : callable(callable) {}
  54. void operator()(const std::string &message) { callable(message); }
  55. };
  56. _Callback callback(callable);
  57. _dispatch(callback);
  58. }
  59. template <class Callable>
  60. void dispatchBinary(Callable callable)
  61. // For callbacks that accept a std::vector<uint8_t> argument.
  62. { // N.B. this is compatible with both C++11 lambdas, functors and C function pointers
  63. struct _Callback : public BytesCallback_Imp
  64. {
  65. Callable &callable;
  66. _Callback(Callable &callable) : callable(callable) {}
  67. void operator()(const std::vector<uint8_t> &message) { callable(message); }
  68. };
  69. _Callback callback(callable);
  70. _dispatchBinary(callback);
  71. }
  72. protected:
  73. virtual void _dispatch(Callback_Imp &callable) = 0;
  74. virtual void _dispatchBinary(BytesCallback_Imp &callable) = 0;
  75. };
  76. } // namespace easywsclient
  77. #endif /* EASYWSCLIENT_HPP_20120819_MIOFVASDTNUASZDQPLFD */