IXWebSocket/ixwebsocket/IXWebSocketServer.h

49 lines
1004 B
C
Raw Normal View History

2018-12-30 08:15:27 +01:00
/*
* IXWebSocketServer.h
* Author: Benjamin Sergeant
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
*/
#pragma once
#include <utility> // pair
#include <string>
2019-01-01 22:47:25 +01:00
#include <set>
#include <thread>
2019-01-01 22:47:25 +01:00
#include <mutex>
#include <functional>
#include "IXWebSocket.h"
2018-12-30 08:15:27 +01:00
namespace ix
{
using OnConnectionCallback = std::function<void(WebSocket&)>;
2018-12-30 08:15:27 +01:00
class WebSocketServer {
public:
WebSocketServer(int port = 8080, int backlog = 5);
2018-12-30 08:15:27 +01:00
virtual ~WebSocketServer();
void setOnConnectionCallback(const OnConnectionCallback& callback);
std::pair<bool, std::string> listen();
void run();
2018-12-30 08:15:27 +01:00
2019-01-01 22:47:25 +01:00
// FIXME: need mutex
std::set<WebSocket*> getClients() { return _clients; }
private:
void handleConnection(int fd);
2018-12-30 08:15:27 +01:00
int _port;
int _backlog;
OnConnectionCallback _onConnectionCallback;
// socket for accepting connections
int _serverFd;
2019-01-01 22:47:25 +01:00
std::set<WebSocket*> _clients;
2018-12-30 08:15:27 +01:00
};
}