mirror of
https://github.com/EQEmu/Server.git
synced 2026-05-17 03:08:26 +00:00
Implement a basic websockets server
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
#include "websocket_server_connection.h"
|
||||
|
||||
#include "../json/json.h"
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <exception>
|
||||
|
||||
namespace EQ
|
||||
{
|
||||
namespace Net
|
||||
{
|
||||
struct WebsocketLoginStatus
|
||||
{
|
||||
bool logged_in;
|
||||
std::string account_name;
|
||||
uint32 account_id;
|
||||
int status;
|
||||
};
|
||||
|
||||
class WebsocketException : public std::exception
|
||||
{
|
||||
public:
|
||||
WebsocketException(const std::string &msg)
|
||||
: _msg(msg.empty() ? "Unknown Error" : msg) { }
|
||||
|
||||
~WebsocketException() throw() {}
|
||||
|
||||
virtual char const *what() const throw() {
|
||||
return _msg.c_str();
|
||||
}
|
||||
private:
|
||||
const std::string _msg;
|
||||
};
|
||||
|
||||
class WebsocketServer
|
||||
{
|
||||
public:
|
||||
typedef std::function<Json::Value(WebsocketServerConnection*, const Json::Value&)> MethodHandler;
|
||||
typedef std::function<WebsocketLoginStatus(WebsocketServerConnection*, const std::string&, const std::string&)> LoginHandler;
|
||||
|
||||
WebsocketServer(const std::string &addr, int port);
|
||||
~WebsocketServer();
|
||||
|
||||
void SetMethodHandler(const std::string& method, MethodHandler handler, int required_status);
|
||||
void SetLoginHandler(LoginHandler handler);
|
||||
void DispatchEvent(const std::string& evt, Json::Value data = Json::Value(), int required_status = 0);
|
||||
private:
|
||||
void ReleaseConnection(WebsocketServerConnection *connection);
|
||||
Json::Value HandleRequest(WebsocketServerConnection *connection, const std::string& method, const Json::Value ¶ms);
|
||||
|
||||
Json::Value Login(WebsocketServerConnection *connection, const Json::Value ¶ms);
|
||||
Json::Value Subscribe(WebsocketServerConnection *connection, const Json::Value ¶ms);
|
||||
Json::Value Unsubscribe(WebsocketServerConnection *connection, const Json::Value ¶ms);
|
||||
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> _impl;
|
||||
|
||||
friend class WebsocketServerConnection;
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user