2019-10-13 22:37:34 +02:00
|
|
|
/*
|
|
|
|
* IXWebSocketServerTest.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2019 Machine Zone. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "IXTest.h"
|
|
|
|
#include "catch.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
#include <ixwebsocket/IXSocket.h>
|
|
|
|
#include <ixwebsocket/IXSocketFactory.h>
|
|
|
|
#include <ixwebsocket/IXWebSocket.h>
|
|
|
|
#include <ixwebsocket/IXWebSocketServer.h>
|
|
|
|
|
|
|
|
using namespace ix;
|
|
|
|
|
|
|
|
bool startServer(ix::WebSocketServer& server, std::string& subProtocols)
|
|
|
|
{
|
2020-07-24 21:49:36 +02:00
|
|
|
server.setOnClientMessageCallback(
|
|
|
|
[&server, &subProtocols](std::shared_ptr<ConnectionState> connectionState,
|
|
|
|
WebSocket& webSocket,
|
|
|
|
const ix::WebSocketMessagePtr& msg) {
|
2020-08-28 23:55:40 +02:00
|
|
|
auto remoteIp = connectionState->getRemoteIp();
|
2020-07-24 21:49:36 +02:00
|
|
|
if (msg->type == ix::WebSocketMessageType::Open)
|
|
|
|
{
|
|
|
|
TLogger() << "New connection";
|
|
|
|
TLogger() << "remote ip: " << remoteIp;
|
|
|
|
TLogger() << "id: " << connectionState->getId();
|
|
|
|
TLogger() << "Uri: " << msg->openInfo.uri;
|
|
|
|
TLogger() << "Headers:";
|
|
|
|
for (auto it : msg->openInfo.headers)
|
|
|
|
{
|
|
|
|
TLogger() << it.first << ": " << it.second;
|
|
|
|
}
|
2019-10-13 22:37:34 +02:00
|
|
|
|
2020-07-24 21:49:36 +02:00
|
|
|
subProtocols = msg->openInfo.headers["Sec-WebSocket-Protocol"];
|
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Close)
|
|
|
|
{
|
|
|
|
log("Closed connection");
|
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Message)
|
|
|
|
{
|
|
|
|
for (auto&& client : server.getClients())
|
|
|
|
{
|
|
|
|
if (client.get() != &webSocket)
|
2019-10-13 22:37:34 +02:00
|
|
|
{
|
2020-07-24 21:49:36 +02:00
|
|
|
client->sendBinary(msg->str);
|
2019-10-13 22:37:34 +02:00
|
|
|
}
|
2020-07-24 21:49:36 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-13 22:37:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
auto res = server.listen();
|
|
|
|
if (!res.first)
|
|
|
|
{
|
|
|
|
log(res.second);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
server.start();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("subprotocol", "[websocket_subprotocol]")
|
|
|
|
{
|
|
|
|
SECTION("Connect to the server")
|
|
|
|
{
|
|
|
|
int port = getFreePort();
|
|
|
|
ix::WebSocketServer server(port);
|
|
|
|
|
|
|
|
std::string subProtocols;
|
|
|
|
startServer(server, subProtocols);
|
|
|
|
|
|
|
|
std::atomic<bool> connected(false);
|
|
|
|
ix::WebSocket webSocket;
|
|
|
|
webSocket.setOnMessageCallback([&connected](const ix::WebSocketMessagePtr& msg) {
|
|
|
|
if (msg->type == ix::WebSocketMessageType::Open)
|
|
|
|
{
|
|
|
|
connected = true;
|
|
|
|
log("Client connected");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
webSocket.addSubProtocol("json");
|
|
|
|
webSocket.addSubProtocol("msgpack");
|
|
|
|
|
|
|
|
std::string url;
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "ws://127.0.0.1:" << port;
|
|
|
|
url = ss.str();
|
|
|
|
|
|
|
|
webSocket.setUrl(url);
|
|
|
|
webSocket.start();
|
|
|
|
|
2020-05-08 19:03:18 +02:00
|
|
|
// Give us 3 seconds to connect
|
2019-10-13 22:37:34 +02:00
|
|
|
int attempts = 0;
|
|
|
|
while (!connected)
|
|
|
|
{
|
2020-05-08 19:03:18 +02:00
|
|
|
REQUIRE(attempts++ < 300);
|
2019-10-13 22:37:34 +02:00
|
|
|
ix::msleep(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
webSocket.stop();
|
|
|
|
server.stop();
|
|
|
|
|
|
|
|
REQUIRE(subProtocols == "json,msgpack");
|
|
|
|
}
|
|
|
|
}
|