(ixwebsocket server) change legacy api with 2 nested callbacks, so that the first api takes a weak_ptr<WebSocket> as its first argument

This commit is contained in:
Benjamin Sergeant
2020-07-25 11:42:07 -07:00
parent 0388459bd0
commit 847fc142d1
5 changed files with 70 additions and 48 deletions

View File

@ -53,28 +53,35 @@ namespace ix
};
server.setConnectionStateFactory(factory);
server.setOnConnectionCallback(
[remoteUrl, verbose](std::shared_ptr<ix::WebSocket> webSocket,
std::shared_ptr<ConnectionState> connectionState,
std::unique_ptr<ConnectionInfo> connectionInfo) {
auto state = std::dynamic_pointer_cast<ProxyConnectionState>(connectionState);
auto remoteIp = connectionInfo->remoteIp;
server.setOnConnectionCallback([remoteUrl,
verbose](std::weak_ptr<ix::WebSocket> webSocket,
std::shared_ptr<ConnectionState> connectionState,
std::unique_ptr<ConnectionInfo> connectionInfo) {
auto state = std::dynamic_pointer_cast<ProxyConnectionState>(connectionState);
auto remoteIp = connectionInfo->remoteIp;
// Server connection
state->webSocket().setOnMessageCallback(
[webSocket, state, remoteIp, verbose](const WebSocketMessagePtr& msg) {
if (msg->type == ix::WebSocketMessageType::Close)
// Server connection
state->webSocket().setOnMessageCallback(
[webSocket, state, remoteIp, verbose](const WebSocketMessagePtr& msg) {
if (msg->type == ix::WebSocketMessageType::Close)
{
state->setTerminated();
}
else if (msg->type == ix::WebSocketMessageType::Message)
{
auto ws = webSocket.lock();
if (ws)
{
state->setTerminated();
ws->send(msg->str, msg->binary);
}
else if (msg->type == ix::WebSocketMessageType::Message)
{
webSocket->send(msg->str, msg->binary);
}
});
}
});
// Client connection
webSocket->setOnMessageCallback(
// Client connection
auto ws = webSocket.lock();
if (ws)
{
ws->setOnMessageCallback(
[state, remoteUrl, verbose](const WebSocketMessagePtr& msg) {
if (msg->type == ix::WebSocketMessageType::Open)
{
@ -101,7 +108,8 @@ namespace ix
state->webSocket().send(msg->str, msg->binary);
}
});
});
}
});
auto res = server.listen();
if (!res.first)

View File

@ -23,7 +23,7 @@ namespace ix
{
public:
using OnConnectionCallback =
std::function<void(std::shared_ptr<WebSocket>,
std::function<void(std::weak_ptr<WebSocket>,
std::shared_ptr<ConnectionState>,
std::unique_ptr<ConnectionInfo> connectionInfo)>;

View File

@ -6,4 +6,4 @@
#pragma once
#define IX_WEBSOCKET_VERSION "9.10.7"
#define IX_WEBSOCKET_VERSION "10.0.0"