IXWebSocket/test/cmd_websocket_chat.cpp

270 lines
7.9 KiB
C++
Raw Normal View History

2018-12-30 03:33:15 +01:00
/*
* cmd_websocket_chat.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2017 Machine Zone. All rights reserved.
*/
#include <iostream>
#include <sstream>
#include <queue>
#include <ixwebsocket/IXWebSocket.h>
2019-01-02 01:34:05 +01:00
#include <ixwebsocket/IXWebSocketServer.h>
2018-12-30 03:33:15 +01:00
#include "msgpack11.hpp"
#include "IXTest.h"
#include "catch.hpp"
using msgpack11::MsgPack;
using namespace ix;
namespace
{
class WebSocketChat
{
public:
WebSocketChat(const std::string& user,
const std::string& session);
void subscribe(const std::string& channel);
void start();
void stop();
bool isReady() const;
void sendMessage(const std::string& text);
size_t getReceivedMessagesCount() const;
std::string encodeMessage(const std::string& text);
std::pair<std::string, std::string> decodeMessage(const std::string& str);
private:
std::string _user;
std::string _session;
ix::WebSocket _webSocket;
std::queue<std::string> _receivedQueue;
};
WebSocketChat::WebSocketChat(const std::string& user,
const std::string& session) :
_user(user),
_session(session)
{
;
}
size_t WebSocketChat::getReceivedMessagesCount() const
{
return _receivedQueue.size();
}
bool WebSocketChat::isReady() const
{
return _webSocket.getReadyState() == ix::WebSocket_ReadyState_Open;
}
void WebSocketChat::stop()
{
_webSocket.stop();
}
void WebSocketChat::start()
{
std::string url("ws://localhost:8090/");
2018-12-30 03:33:15 +01:00
_webSocket.setUrl(url);
std::stringstream ss;
log(std::string("Connecting to url: ") + url);
_webSocket.setOnMessageCallback(
[this](ix::WebSocketMessageType messageType,
const std::string& str,
size_t wireSize,
const ix::WebSocketErrorInfo& error,
const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo)
2018-12-30 03:33:15 +01:00
{
std::stringstream ss;
if (messageType == ix::WebSocket_MessageType_Open)
{
ss << "cmd_websocket_chat: user "
<< _user
<< " Connected !";
log(ss.str());
}
else if (messageType == ix::WebSocket_MessageType_Close)
{
ss << "cmd_websocket_chat: user "
<< _user
<< " disconnected !";
log(ss.str());
}
else if (messageType == ix::WebSocket_MessageType_Message)
{
auto result = decodeMessage(str);
// Our "chat" / "broacast" node.js server does not send us
// the messages we send, so we don't need to have a msg_user != user
// as we do for the satori chat example.
// store text
_receivedQueue.push(result.second);
ss << std::endl
<< result.first << " > " << result.second
<< std::endl
<< _user << " > ";
log(ss.str());
}
else if (messageType == ix::WebSocket_MessageType_Error)
{
ss << "cmd_websocket_chat: Error ! " << error.reason;
log(ss.str());
}
else
{
// FIXME: missing ping/pong messages
ss << "Invalid ix::WebSocketMessageType";
log(ss.str());
}
});
_webSocket.start();
}
std::pair<std::string, std::string> WebSocketChat::decodeMessage(const std::string& str)
{
std::string errMsg;
MsgPack msg = MsgPack::parse(str, errMsg);
std::string msg_user = msg["user"].string_value();
std::string msg_text = msg["text"].string_value();
return std::pair<std::string, std::string>(msg_user, msg_text);
}
std::string WebSocketChat::encodeMessage(const std::string& text)
{
std::map<MsgPack, MsgPack> obj;
obj["user"] = _user;
obj["text"] = text;
MsgPack msg(obj);
std::string output = msg.dump();
return output;
}
void WebSocketChat::sendMessage(const std::string& text)
{
_webSocket.send(encodeMessage(text));
}
2019-01-02 01:34:05 +01:00
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::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo)
2019-01-02 01:34:05 +01:00
{
if (messageType == ix::WebSocket_MessageType_Open)
{
2019-01-08 03:04:28 +01:00
Logger() << "New connection";
Logger() << "Uri: " << openInfo.uri;
Logger() << "Headers:";
for (auto it : openInfo.headers)
2019-01-02 01:34:05 +01:00
{
2019-01-08 03:04:28 +01:00
Logger() << it.first << ": " << it.second;
2019-01-02 01:34:05 +01:00
}
}
else if (messageType == ix::WebSocket_MessageType_Close)
{
2019-01-08 03:04:28 +01:00
log("Closed connection");
2019-01-02 01:34:05 +01:00
}
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)
{
2019-01-08 03:04:28 +01:00
log(res.second);
2019-01-02 01:34:05 +01:00
return false;
}
server.start();
return true;
}
2018-12-30 03:33:15 +01:00
}
TEST_CASE("Websocket_chat", "[websocket_chat]")
2018-12-30 03:33:15 +01:00
{
SECTION("Exchange and count sent/received messages.")
{
ix::setupWebSocketTrafficTrackerCallback();
int port = 8090;
2019-01-02 01:34:05 +01:00
ix::WebSocketServer server(port);
REQUIRE(startServer(server));
2019-01-02 01:34:05 +01:00
2018-12-30 03:33:15 +01:00
std::string session = ix::generateSessionId();
WebSocketChat chatA("jean", session);
WebSocketChat chatB("paul", session);
chatA.start();
chatB.start();
// Wait for all chat instance to be ready
while (true)
{
if (chatA.isReady() && chatB.isReady()) break;
ix::msleep(10);
}
REQUIRE(server.getClients().size() == 2);
2018-12-30 03:33:15 +01:00
// Add a bit of extra time, for the subscription to be active
ix::msleep(200);
chatA.sendMessage("from A1");
chatA.sendMessage("from A2");
chatA.sendMessage("from A3");
chatB.sendMessage("from B1");
chatB.sendMessage("from B2");
// Give us 1s for all messages to be received
ix::msleep(1000);
chatA.stop();
chatB.stop();
REQUIRE(chatA.getReceivedMessagesCount() == 2);
REQUIRE(chatB.getReceivedMessagesCount() == 3);
// Give us 500ms for the server to notice that clients went away
ix::msleep(500);
REQUIRE(server.getClients().size() == 0);
2018-12-30 03:33:15 +01:00
ix::reportWebSocketTraffic();
}
}