/* * 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, std::unique_ptr connectionInfo)>; using OnClientMessageCallback = std::function, ConnectionInfo&, 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(); const static int kDefaultHandShakeTimeoutSecs; 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, std::unique_ptr connectionInfo); virtual size_t getConnectedClientsCount() final; }; } // namespace ix