NAP
websocketserver.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 "iwebsocketserverendpoint.h"
9 #include "websocketinterface.h"
10 
11 // External Includes
12 #include <rtti/factory.h>
13 #include <nap/resourceptr.h>
14 
15 namespace nap
16 {
22  class NAPAPI IWebSocketServer : public WebSocketInterface
23  {
24  template<typename config>
26 
27  RTTI_ENABLE(WebSocketInterface)
28  public:
34 
40  virtual bool init(utility::ErrorState& errorState) override;
41 
45  virtual void onDestroy() override;
46 
48 
49  protected:
50  // Called by web-socket server endpoint when a new message is received
51  virtual void onMessageReceived(const WebSocketConnection& connection, const WebSocketMessage& message) = 0;
52 
53  // Called by web-socket server endpoint when a client connection opened
54  virtual void onConnectionOpened(const WebSocketConnection& connection) = 0;
55 
56  // Called by web-socket server endpoint when a client connection closed
57  virtual void onConnectionClosed(const WebSocketConnection& connection, int code, const std::string& reason) = 0;
58 
59  // Called by web-socket server endpoint when a client connection failed to establish
60  virtual void onConnectionFailed(const WebSocketConnection& connection, int code, const std::string& reason) = 0;
61  };
62 
63 
65  // WebSocketServer
67 
77  class NAPAPI WebSocketServer : public IWebSocketServer
78  {
79  friend class WebSocketService;
80  RTTI_ENABLE(IWebSocketServer)
81  public:
87 
95  bool send(const WebSocketConnection& connection, const WebSocketMessage& message, nap::utility::ErrorState& error);
96 
106  bool send(const WebSocketConnection& connection, void const* payload, int length, EWebSocketOPCode code, nap::utility::ErrorState& error);
107 
116  bool send(const WebSocketConnection& connection, const std::string& message, EWebSocketOPCode code, nap::utility::ErrorState& error);
117 
124  bool broadcast(const WebSocketMessage& message, nap::utility::ErrorState& error);
125 
134  bool broadcast(void const* payload, int length, EWebSocketOPCode code, nap::utility::ErrorState& error);
135 
136  private:
142  virtual void onConnectionOpened(const WebSocketConnection& connection) override;
143 
151  virtual void onConnectionClosed(const WebSocketConnection& connection, int code, const std::string& reason) override;
152 
160  virtual void onConnectionFailed(const WebSocketConnection& connection, int code, const std::string& reason) override;
161 
168  virtual void onMessageReceived(const WebSocketConnection& connection, const WebSocketMessage& message) override;
169  };
170 
171  // Object creator used for constructing the web-socket server
173 }
nap::WebSocketConnection
Definition: websocketconnection.h:27
nap::WebSocketServer
Definition: websocketserver.h:77
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::WebSocketServerEndPointSetup
Definition: websocketconnection.h:16
nap::WebSocketMessage
Definition: websocketmessage.h:29
nap
Definition: templateapp.h:17
nap::IWebSocketServer::mEndPoint
ResourcePtr< IWebSocketServerEndPoint > mEndPoint
Property: 'EndPoint' the server endpoint that manages all client connections.
Definition: websocketserver.h:47
nap::WebSocketInterface
Definition: websocketinterface.h:28
nap::IWebSocketServer
Definition: websocketserver.h:22
nap::EWebSocketOPCode
EWebSocketOPCode
Definition: websocketutils.h:29