/* * IXWebSocketServer.h * Author: Benjamin Sergeant * Copyright (c) 2018 Machine Zone, Inc. All rights reserved. */ #pragma once #include "IXSocketServer.h" #include "IXWebSocket.h" #include #include #include #include #include #include #include #include // pair namespace ix { class WebSocketServer : public SocketServer { public: using OnConnectionCallback = std::function, std::shared_ptr)>; using OnClientMessageCallback = std::function, WebSocket&, const WebSocketMessagePtr&)>; WebSocketServer(int port = SocketServer::kDefaultPort, const std::string& host = SocketServer::kDefaultHost, int backlog = SocketServer::kDefaultTcpBacklog, size_t maxConnections = SocketServer::kDefaultMaxConnections, int handshakeTimeoutSecs = WebSocketServer::kDefaultHandShakeTimeoutSecs, int addressFamily = SocketServer::kDefaultAddressFamily); virtual ~WebSocketServer(); virtual void stop() final; void enablePong(); void disablePong(); void disablePerMessageDeflate(); void setOnConnectionCallback(const OnConnectionCallback& callback); void setOnClientMessageCallback(const OnClientMessageCallback& callback); // Get all the connected clients std::set> getClients(); void makeBroadcastServer(); bool listenAndStart(); const static int kDefaultHandShakeTimeoutSecs; int getHandshakeTimeoutSecs(); bool isPongEnabled(); bool isPerMessageDeflateEnabled(); private: // Member variables int _handshakeTimeoutSecs; bool _enablePong; bool _enablePerMessageDeflate; OnConnectionCallback _onConnectionCallback; OnClientMessageCallback _onClientMessageCallback; std::mutex _clientsMutex; std::set> _clients; const static bool kDefaultEnablePong; // Methods virtual void handleConnection(std::unique_ptr socket, std::shared_ptr connectionState); virtual size_t getConnectedClientsCount() final; protected: void handleUpgrade(std::unique_ptr socket, std::shared_ptr connectionState, HttpRequestPtr request = nullptr); }; } // namespace ix