NAP
websocketclient.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Local Includes
8 #include "iwebsocketclientendpoint.h"
9 #include "websocketinterface.h"
10 #include "websocketticket.h"
11 
12 // External Includes
13 #include <nap/resource.h>
14 #include <nap/resourceptr.h>
15 
16 namespace nap
17 {
34  class NAPAPI IWebSocketClient : public WebSocketInterface
35  {
36  template<typename config>
38 
39  template<typename config>
40  friend class WebSocketClientWrapper;
41 
42  RTTI_ENABLE(WebSocketInterface)
43  public:
49 
58  virtual bool init(utility::ErrorState& errorState) override;
59 
63  virtual void onDestroy() override;
64 
68  bool isConnected() const;
69 
79  bool reconnect(utility::ErrorState& error);
80 
84  const WebSocketConnection& getConnection() const { return mConnection; }
85 
87  ResourcePtr<WebSocketTicket> mTicket = nullptr;
88  std::string mURI;
89 
90  protected:
92 
96  virtual void onConnectionOpened() = 0;
97 
103  virtual void onConnectionClosed(int code, const std::string& reason) = 0;
104 
110  virtual void onConnectionFailed(int code, const std::string& reason) = 0;
111 
116  virtual void onMessageReceived(const WebSocketMessage& msg) = 0;
117 
118  private:
119  // Called by web-socket client endpoint when the connection is opened
120  void connectionOpened();
121  // Called by web-socket client endpoint when the connection is closed
122  void connectionClosed(int code, const std::string& reason);
123  // Called by web-socket client endpoint when the connection failed to establish
124  void connectionFailed(int code, const std::string& reason);
125  // Called by web-socket client endpoint when a new message is received
126  void messageReceived(const WebSocketMessage& msg);
127 
128  std::atomic<bool> mOpen = { false };
129  };
130 
131 
133  // WebSocketClient
135 
150  class NAPAPI WebSocketClient : public IWebSocketClient
151  {
152  RTTI_ENABLE(IWebSocketClient)
153  public:
159 
167  bool send(const std::string& message, EWebSocketOPCode code, nap::utility::ErrorState& error);
168 
178  bool send(void const* payload, int length, EWebSocketOPCode code, nap::utility::ErrorState& error);
179 
186  bool send(const WebSocketMessage& message, nap::utility::ErrorState& error);
187 
188  private:
193  virtual void onConnectionOpened() override;
194 
201  virtual void onConnectionClosed(int code, const std::string& reason) override;
202 
209  virtual void onConnectionFailed(int code, const std::string& reason) override;
210 
216  virtual void onMessageReceived(const WebSocketMessage& msg) override;
217  };
218 
219  // Object creator used for constructing the web-socket client
221 }
nap::WebSocketClientEndPointSetup
Definition: websocketclientendpoint.h:39
nap::WebSocketConnection
Definition: websocketconnection.h:27
nap::IWebSocketClient::getConnection
const WebSocketConnection & getConnection() const
Definition: websocketclient.h:84
nap::WebSocketClientWrapper
Definition: websocketclientendpoint.h:20
nap::IWebSocketClient::mConnection
WebSocketConnection mConnection
Web-socket connection.
Definition: websocketclient.h:91
nap::rtti::ObjectPtr
Definition: objectptr.h:154
nap::WebSocketService
Definition: websocketservice.h:23
nap::rtti::ObjectCreator
Definition: factory.h:49
nap::utility::ErrorState
Definition: errorstate.h:19
nap::WebSocketClient
Definition: websocketclient.h:150
nap::WebSocketMessage
Definition: websocketmessage.h:29
nap::IWebSocketClient::mEndPoint
ResourcePtr< IWebSocketClientEndPoint > mEndPoint
Property: 'EndPoint' the client endpoint that manages all connections.
Definition: websocketclient.h:86
nap::IWebSocketClient
Definition: websocketclient.h:34
nap
Definition: templateapp.h:17
nap::WebSocketInterface
Definition: websocketinterface.h:28
nap::EWebSocketOPCode
EWebSocketOPCode
Definition: websocketutils.h:29
nap::IWebSocketClient::mURI
std::string mURI
Property: "URI" Server URI to open connection to.
Definition: websocketclient.h:88