sio_socket.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef SIO_SOCKET_H
  2. #define SIO_SOCKET_H
  3. #include "sio_message.h"
  4. #include <functional>
  5. namespace sio
  6. {
  7. class event_adapter;
  8. class event
  9. {
  10. public:
  11. const std::string& get_nsp() const;
  12. const std::string& get_name() const;
  13. const message::ptr& get_message() const;
  14. const message::list& get_messages() const;
  15. bool need_ack() const;
  16. void put_ack_message(message::list const& ack_message);
  17. message::list const& get_ack_message() const;
  18. protected:
  19. event(std::string const& nsp,std::string const& name,message::list const& messages,bool need_ack);
  20. event(std::string const& nsp,std::string const& name,message::list&& messages,bool need_ack);
  21. message::list& get_ack_message_impl();
  22. private:
  23. const std::string m_nsp;
  24. const std::string m_name;
  25. const message::list m_messages;
  26. const bool m_need_ack;
  27. message::list m_ack_message;
  28. friend class event_adapter;
  29. };
  30. class client_impl;
  31. class packet;
  32. //The name 'socket' is taken from concept of official socket.io.
  33. class socket
  34. {
  35. public:
  36. typedef std::function<void(const std::string& name,message::ptr const& message,bool need_ack, message::list& ack_message)> event_listener_aux;
  37. typedef std::function<void(event& event)> event_listener;
  38. typedef std::function<void(message::ptr const& message)> error_listener;
  39. typedef std::shared_ptr<socket> ptr;
  40. ~socket();
  41. void on(std::string const& event_name,event_listener const& func);
  42. void on(std::string const& event_name,event_listener_aux const& func);
  43. void off(std::string const& event_name);
  44. void off_all();
  45. void close();
  46. void on_error(error_listener const& l);
  47. void off_error();
  48. void emit(std::string const& name, message::list const& msglist = nullptr, std::function<void (message::list const&)> const& ack = nullptr);
  49. std::string const& get_namespace() const;
  50. protected:
  51. socket(client_impl*,std::string const&);
  52. void on_connected();
  53. void on_close();
  54. void on_open();
  55. void on_disconnect();
  56. void on_message_packet(packet const& p);
  57. friend class client_impl;
  58. private:
  59. //disable copy constructor and assign operator.
  60. socket(socket const&){}
  61. void operator=(socket const&){}
  62. class impl;
  63. impl *m_impl;
  64. };
  65. }
  66. #endif // SIO_SOCKET_H