unittest starts a server

This commit is contained in:
Benjamin Sergeant 2019-01-01 16:34:05 -08:00
parent e5937638d4
commit 06177afd6a
3 changed files with 64 additions and 2 deletions

View File

@ -10,8 +10,11 @@ RUN apt-get -y install procps
RUN apt-get -y install lsof
RUN apt-get -y install libz-dev
RUN apt-get -y install vim
RUN apt-get -y install make
RUN apt-get -y install cmake
COPY . .
WORKDIR examples/ws_connect
RUN ["sh", "build_linux.sh"]
#WORKDIR examples/ws_connect
#RUN ["sh", "build_linux.sh"]
RUN ["make", "test"]

View File

@ -16,6 +16,8 @@ build:
(cd examples/ping_pong ; mkdir -p build ; cd build ; cmake .. ; make)
(cd examples/ws_connect ; mkdir -p build ; cd build ; cmake .. ; make)
# That target is used to start a node server, but isn't required as we have
# a builtin C++ server started in the unittest now
test_server:
(cd test && npm i ws && node broadcast-server.js)
test:

View File

@ -13,6 +13,7 @@
#include <sstream>
#include <queue>
#include <ixwebsocket/IXWebSocket.h>
#include <ixwebsocket/IXWebSocketServer.h>
#include "msgpack11.hpp"
#include "IXTest.h"
@ -171,6 +172,58 @@ namespace
{
_webSocket.send(encodeMessage(text));
}
bool startServer(ix::WebSocketServer& server)
{
server.setOnConnectionCallback(
[&server](std::shared_ptr<ix::WebSocket> webSocket)
{
webSocket->setOnMessageCallback(
[webSocket, &server](ix::WebSocketMessageType messageType,
const std::string& str,
size_t wireSize,
const ix::WebSocketErrorInfo& error,
const ix::WebSocketCloseInfo& closeInfo,
const ix::WebSocketHttpHeaders& headers)
{
if (messageType == ix::WebSocket_MessageType_Open)
{
std::cerr << "New connection" << std::endl;
std::cerr << "Headers:" << std::endl;
for (auto it : headers)
{
std::cerr << it.first << ": " << it.second << std::endl;
}
}
else if (messageType == ix::WebSocket_MessageType_Close)
{
std::cerr << "Closed connection" << std::endl;
}
else if (messageType == ix::WebSocket_MessageType_Message)
{
for (auto&& client : server.getClients())
{
if (client != webSocket)
{
client->send(str);
}
}
}
}
);
}
);
auto res = server.listen();
if (!res.first)
{
std::cerr << res.second << std::endl;
return false;
}
server.start();
return true;
}
}
TEST_CASE("Websocket chat", "[websocket_chat]")
@ -179,6 +232,10 @@ TEST_CASE("Websocket chat", "[websocket_chat]")
{
ix::setupWebSocketTrafficTrackerCallback();
int port = 8080;
ix::WebSocketServer server(port);
startServer(server);
std::string session = ix::generateSessionId();
WebSocketChat chatA("jean", session);
WebSocketChat chatB("paul", session);