2018-12-30 03:33:15 +01:00
|
|
|
/*
|
|
|
|
* cmd_websocket_chat.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2017 Machine Zone. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2019-01-29 00:14:49 +01:00
|
|
|
//
|
|
|
|
// Simple chat program that talks to the node.js server at
|
|
|
|
// websocket_chat_server/broacast-server.js
|
|
|
|
//
|
|
|
|
|
2018-12-30 03:33:15 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
2019-02-16 19:31:55 +01:00
|
|
|
#include <vector>
|
|
|
|
#include <mutex>
|
2018-12-30 03:33:15 +01:00
|
|
|
#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,
|
2019-01-29 00:14:49 +01:00
|
|
|
const std::string& session,
|
|
|
|
int port);
|
2018-12-30 03:33:15 +01:00
|
|
|
|
|
|
|
void subscribe(const std::string& channel);
|
|
|
|
void start();
|
|
|
|
void stop();
|
|
|
|
bool isReady() const;
|
|
|
|
|
|
|
|
void sendMessage(const std::string& text);
|
|
|
|
size_t getReceivedMessagesCount() const;
|
2019-02-16 19:31:55 +01:00
|
|
|
const std::vector<std::string>& getReceivedMessages() const;
|
2018-12-30 03:33:15 +01:00
|
|
|
|
|
|
|
std::string encodeMessage(const std::string& text);
|
|
|
|
std::pair<std::string, std::string> decodeMessage(const std::string& str);
|
2019-02-16 19:31:55 +01:00
|
|
|
void appendMessage(const std::string& message);
|
2018-12-30 03:33:15 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::string _user;
|
|
|
|
std::string _session;
|
2019-01-29 00:14:49 +01:00
|
|
|
int _port;
|
2018-12-30 03:33:15 +01:00
|
|
|
|
|
|
|
ix::WebSocket _webSocket;
|
|
|
|
|
2019-02-16 19:31:55 +01:00
|
|
|
std::vector<std::string> _receivedMessages;
|
|
|
|
mutable std::mutex _mutex;
|
2018-12-30 03:33:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
WebSocketChat::WebSocketChat(const std::string& user,
|
2019-01-29 00:14:49 +01:00
|
|
|
const std::string& session,
|
|
|
|
int port) :
|
2018-12-30 03:33:15 +01:00
|
|
|
_user(user),
|
2019-01-29 00:14:49 +01:00
|
|
|
_session(session),
|
|
|
|
_port(port)
|
2018-12-30 03:33:15 +01:00
|
|
|
{
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t WebSocketChat::getReceivedMessagesCount() const
|
|
|
|
{
|
2019-02-16 19:31:55 +01:00
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
|
|
return _receivedMessages.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<std::string>& WebSocketChat::getReceivedMessages() const
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
|
|
return _receivedMessages;
|
|
|
|
}
|
|
|
|
|
2019-02-21 03:59:07 +01:00
|
|
|
void WebSocketChat::appendMessage(const std::string& message)
|
2019-02-16 19:31:55 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
|
|
_receivedMessages.push_back(message);
|
2018-12-30 03:33:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebSocketChat::isReady() const
|
|
|
|
{
|
2019-05-11 23:22:06 +02:00
|
|
|
return _webSocket.getReadyState() == ix::ReadyState::Open;
|
2018-12-30 03:33:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketChat::stop()
|
|
|
|
{
|
|
|
|
_webSocket.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketChat::start()
|
|
|
|
{
|
2019-01-29 00:14:49 +01:00
|
|
|
std::string url;
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2019-05-06 21:22:57 +02:00
|
|
|
ss << "ws://127.0.0.1:"
|
2019-02-21 03:59:07 +01:00
|
|
|
<< _port
|
2019-02-16 19:31:55 +01:00
|
|
|
<< "/"
|
|
|
|
<< _user;
|
2019-01-29 00:14:49 +01:00
|
|
|
|
|
|
|
url = ss.str();
|
|
|
|
}
|
|
|
|
|
2018-12-30 03:33:15 +01:00
|
|
|
_webSocket.setUrl(url);
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
log(std::string("Connecting to url: ") + url);
|
|
|
|
|
|
|
|
_webSocket.setOnMessageCallback(
|
2019-06-09 20:33:17 +02:00
|
|
|
[this](const ix::WebSocketMessagePtr& msg)
|
2018-12-30 03:33:15 +01:00
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2019-06-09 20:33:17 +02:00
|
|
|
if (msg->type == ix::WebSocketMessageType::Open)
|
2018-12-30 03:33:15 +01:00
|
|
|
{
|
|
|
|
ss << "cmd_websocket_chat: user "
|
|
|
|
<< _user
|
|
|
|
<< " Connected !";
|
|
|
|
log(ss.str());
|
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Close)
|
2018-12-30 03:33:15 +01:00
|
|
|
{
|
|
|
|
ss << "cmd_websocket_chat: user "
|
|
|
|
<< _user
|
|
|
|
<< " disconnected !";
|
|
|
|
log(ss.str());
|
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Message)
|
2018-12-30 03:33:15 +01:00
|
|
|
{
|
2019-06-09 20:33:17 +02:00
|
|
|
auto result = decodeMessage(msg->str);
|
2018-12-30 03:33:15 +01:00
|
|
|
|
|
|
|
// 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
|
2019-02-16 19:31:55 +01:00
|
|
|
appendMessage(result.second);
|
|
|
|
|
|
|
|
std::string payload = result.second;
|
|
|
|
if (payload.size() > 2000)
|
|
|
|
{
|
2019-02-21 03:59:07 +01:00
|
|
|
payload = "<message too large>";
|
2019-02-16 19:31:55 +01:00
|
|
|
}
|
2018-12-30 03:33:15 +01:00
|
|
|
|
2019-02-21 03:59:07 +01:00
|
|
|
ss << std::endl
|
2019-02-16 19:31:55 +01:00
|
|
|
<< result.first << " > " << payload
|
2018-12-30 03:33:15 +01:00
|
|
|
<< std::endl
|
|
|
|
<< _user << " > ";
|
|
|
|
log(ss.str());
|
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Error)
|
2018-12-30 03:33:15 +01:00
|
|
|
{
|
2019-06-09 20:33:17 +02:00
|
|
|
ss << "cmd_websocket_chat: Error ! " << msg->errorInfo.reason;
|
2018-12-30 03:33:15 +01:00
|
|
|
log(ss.str());
|
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Ping)
|
2019-03-14 07:09:45 +01:00
|
|
|
{
|
|
|
|
log("cmd_websocket_chat: received ping message");
|
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Pong)
|
2019-03-14 07:09:45 +01:00
|
|
|
{
|
|
|
|
log("cmd_websocket_chat: received pong message");
|
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Fragment)
|
2019-03-14 07:09:45 +01:00
|
|
|
{
|
|
|
|
log("cmd_websocket_chat: received message fragment");
|
|
|
|
}
|
2018-12-30 03:33:15 +01:00
|
|
|
else
|
|
|
|
{
|
2019-03-14 07:09:45 +01:00
|
|
|
ss << "Unexpected ix::WebSocketMessageType";
|
2018-12-30 03:33:15 +01:00
|
|
|
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)
|
|
|
|
{
|
2019-09-03 23:12:40 +02:00
|
|
|
_webSocket.sendBinary(encodeMessage(text));
|
2018-12-30 03:33:15 +01:00
|
|
|
}
|
2019-01-02 01:34:05 +01:00
|
|
|
|
|
|
|
bool startServer(ix::WebSocketServer& server)
|
|
|
|
{
|
|
|
|
server.setOnConnectionCallback(
|
2019-03-21 02:34:24 +01:00
|
|
|
[&server](std::shared_ptr<ix::WebSocket> webSocket,
|
|
|
|
std::shared_ptr<ConnectionState> connectionState)
|
2019-01-02 01:34:05 +01:00
|
|
|
{
|
|
|
|
webSocket->setOnMessageCallback(
|
2019-06-09 20:33:17 +02:00
|
|
|
[webSocket, connectionState, &server](const ix::WebSocketMessagePtr& msg)
|
2019-01-02 01:34:05 +01:00
|
|
|
{
|
2019-06-09 20:33:17 +02:00
|
|
|
if (msg->type == ix::WebSocketMessageType::Open)
|
2019-01-02 01:34:05 +01:00
|
|
|
{
|
2019-01-08 03:04:28 +01:00
|
|
|
Logger() << "New connection";
|
2019-03-21 02:34:24 +01:00
|
|
|
Logger() << "id: " << connectionState->getId();
|
2019-06-09 20:33:17 +02:00
|
|
|
Logger() << "Uri: " << msg->openInfo.uri;
|
2019-01-08 03:04:28 +01:00
|
|
|
Logger() << "Headers:";
|
2019-06-09 20:33:17 +02:00
|
|
|
for (auto it : msg->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
|
|
|
}
|
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Close)
|
2019-01-02 01:34:05 +01:00
|
|
|
{
|
2019-01-08 03:04:28 +01:00
|
|
|
log("Closed connection");
|
2019-01-02 01:34:05 +01:00
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Message)
|
2019-01-02 01:34:05 +01:00
|
|
|
{
|
|
|
|
for (auto&& client : server.getClients())
|
|
|
|
{
|
|
|
|
if (client != webSocket)
|
|
|
|
{
|
2019-09-03 23:12:40 +02:00
|
|
|
client->sendBinary(msg->str);
|
2019-01-02 01:34:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-01-03 05:07:54 +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();
|
|
|
|
|
2019-01-02 07:21:07 +01:00
|
|
|
int port = 8090;
|
2019-01-02 01:34:05 +01:00
|
|
|
ix::WebSocketServer server(port);
|
2019-01-02 07:21:07 +01:00
|
|
|
REQUIRE(startServer(server));
|
2019-01-02 01:34:05 +01:00
|
|
|
|
2018-12-30 03:33:15 +01:00
|
|
|
std::string session = ix::generateSessionId();
|
2019-01-29 00:14:49 +01:00
|
|
|
WebSocketChat chatA("jean", session, port);
|
|
|
|
WebSocketChat chatB("paul", session, port);
|
2018-12-30 03:33:15 +01:00
|
|
|
|
|
|
|
chatA.start();
|
|
|
|
chatB.start();
|
|
|
|
|
|
|
|
// Wait for all chat instance to be ready
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (chatA.isReady() && chatB.isReady()) break;
|
|
|
|
ix::msleep(10);
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:25:15 +01:00
|
|
|
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");
|
|
|
|
|
2019-02-21 03:59:07 +01:00
|
|
|
// Test large messages that needs to be broken into small fragments
|
|
|
|
size_t size = 1 * 1024 * 1024; // ~1Mb
|
2019-02-16 19:31:55 +01:00
|
|
|
std::string bigMessage(size, 'a');
|
|
|
|
chatB.sendMessage(bigMessage);
|
|
|
|
|
2019-02-21 03:59:07 +01:00
|
|
|
log("Sent all messages");
|
|
|
|
|
2019-02-16 19:31:55 +01:00
|
|
|
// Wait until all messages are received. 10s timeout
|
|
|
|
int attempts = 0;
|
|
|
|
while (chatA.getReceivedMessagesCount() != 3 ||
|
|
|
|
chatB.getReceivedMessagesCount() != 3)
|
|
|
|
{
|
|
|
|
REQUIRE(attempts++ < 10);
|
|
|
|
ix::msleep(1000);
|
|
|
|
}
|
2018-12-30 03:33:15 +01:00
|
|
|
|
|
|
|
chatA.stop();
|
|
|
|
chatB.stop();
|
|
|
|
|
2019-02-16 19:31:55 +01:00
|
|
|
REQUIRE(chatA.getReceivedMessagesCount() == 3);
|
2018-12-30 03:33:15 +01:00
|
|
|
REQUIRE(chatB.getReceivedMessagesCount() == 3);
|
|
|
|
|
2019-02-16 19:31:55 +01:00
|
|
|
REQUIRE(chatB.getReceivedMessages()[0] == "from A1");
|
|
|
|
REQUIRE(chatB.getReceivedMessages()[1] == "from A2");
|
|
|
|
REQUIRE(chatB.getReceivedMessages()[2] == "from A3");
|
|
|
|
|
|
|
|
REQUIRE(chatA.getReceivedMessages()[0] == "from B1");
|
|
|
|
REQUIRE(chatA.getReceivedMessages()[1] == "from B2");
|
|
|
|
REQUIRE(chatA.getReceivedMessages()[2].size() == bigMessage.size());
|
|
|
|
|
2019-05-21 18:34:08 +02:00
|
|
|
// Give us 1000ms for the server to notice that clients went away
|
|
|
|
ix::msleep(1000);
|
2019-01-02 06:25:15 +01:00
|
|
|
REQUIRE(server.getClients().size() == 0);
|
|
|
|
|
2018-12-30 03:33:15 +01:00
|
|
|
ix::reportWebSocketTraffic();
|
|
|
|
}
|
|
|
|
}
|