2019-02-23 06:13:56 +01:00
|
|
|
/*
|
|
|
|
* ws_broadcast_server.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <ixwebsocket/IXWebSocketServer.h>
|
|
|
|
|
|
|
|
namespace ix
|
|
|
|
{
|
2019-03-11 00:36:44 +01:00
|
|
|
int ws_broadcast_server_main(int port, const std::string& hostname)
|
2019-02-23 06:13:56 +01:00
|
|
|
{
|
2019-03-11 00:36:44 +01:00
|
|
|
std::cout << "Listening on " << hostname << ":" << port << std::endl;
|
2019-02-23 06:13:56 +01:00
|
|
|
|
2019-03-11 00:36:44 +01:00
|
|
|
ix::WebSocketServer server(port, hostname);
|
2019-02-23 06:13:56 +01:00
|
|
|
|
|
|
|
server.setOnConnectionCallback(
|
2019-03-21 02:34:24 +01:00
|
|
|
[&server](std::shared_ptr<WebSocket> webSocket,
|
|
|
|
std::shared_ptr<ConnectionState> connectionState)
|
2019-02-23 06:13:56 +01:00
|
|
|
{
|
|
|
|
webSocket->setOnMessageCallback(
|
2019-03-21 02:34:24 +01:00
|
|
|
[webSocket, connectionState, &server](ix::WebSocketMessageType messageType,
|
2019-02-23 06:13:56 +01:00
|
|
|
const std::string& str,
|
|
|
|
size_t wireSize,
|
|
|
|
const ix::WebSocketErrorInfo& error,
|
|
|
|
const ix::WebSocketOpenInfo& openInfo,
|
|
|
|
const ix::WebSocketCloseInfo& closeInfo)
|
|
|
|
{
|
|
|
|
if (messageType == ix::WebSocket_MessageType_Open)
|
|
|
|
{
|
|
|
|
std::cerr << "New connection" << std::endl;
|
2019-03-21 02:34:24 +01:00
|
|
|
std::cerr << "id: " << connectionState->getId() << std::endl;
|
2019-02-23 06:13:56 +01:00
|
|
|
std::cerr << "Uri: " << openInfo.uri << std::endl;
|
|
|
|
std::cerr << "Headers:" << std::endl;
|
|
|
|
for (auto it : openInfo.headers)
|
|
|
|
{
|
|
|
|
std::cerr << it.first << ": " << it.second << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (messageType == ix::WebSocket_MessageType_Close)
|
|
|
|
{
|
2019-03-04 22:56:30 +01:00
|
|
|
std::cerr << "Closed connection"
|
|
|
|
<< " code " << closeInfo.code
|
|
|
|
<< " reason " << closeInfo.reason << std::endl;
|
|
|
|
}
|
|
|
|
else if (messageType == ix::WebSocket_MessageType_Error)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Connection error: " << error.reason << std::endl;
|
|
|
|
ss << "#retries: " << error.retries << std::endl;
|
|
|
|
ss << "Wait time(ms): " << error.wait_time << std::endl;
|
|
|
|
ss << "HTTP Status: " << error.http_status << std::endl;
|
|
|
|
std::cerr << ss.str();
|
2019-02-23 06:13:56 +01:00
|
|
|
}
|
2019-03-11 19:12:43 +01:00
|
|
|
else if (messageType == ix::WebSocket_MessageType_Fragment)
|
|
|
|
{
|
|
|
|
std::cerr << "Received message fragment" << std::endl;
|
|
|
|
}
|
2019-02-23 06:13:56 +01:00
|
|
|
else if (messageType == ix::WebSocket_MessageType_Message)
|
|
|
|
{
|
|
|
|
std::cerr << "Received " << wireSize << " bytes" << std::endl;
|
2019-03-04 22:56:30 +01:00
|
|
|
|
2019-02-23 06:13:56 +01:00
|
|
|
for (auto&& client : server.getClients())
|
|
|
|
{
|
|
|
|
if (client != webSocket)
|
|
|
|
{
|
2019-03-12 06:16:55 +01:00
|
|
|
client->send(str,
|
|
|
|
[](int current, int total) -> bool
|
|
|
|
{
|
|
|
|
std::cerr << "Step " << current
|
|
|
|
<< " out of " << total << std::endl;
|
|
|
|
return true;
|
|
|
|
});
|
2019-03-14 07:09:45 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
size_t bufferedAmount = client->bufferedAmount();
|
|
|
|
std::cerr << bufferedAmount << " bytes left to be sent" << std::endl;
|
|
|
|
|
|
|
|
std::chrono::duration<double, std::milli> duration(10);
|
|
|
|
std::this_thread::sleep_for(duration);
|
|
|
|
} while (client->bufferedAmount() != 0);
|
2019-02-23 06:13:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
auto res = server.listen();
|
|
|
|
if (!res.first)
|
|
|
|
{
|
|
|
|
std::cerr << res.second << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
server.start();
|
|
|
|
server.wait();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|