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 <ixwebsocket/IXWebSocketServer.h>
|
2019-09-23 19:25:23 +02:00
|
|
|
#include <sstream>
|
2019-02-23 06:13:56 +01:00
|
|
|
|
|
|
|
namespace ix
|
|
|
|
{
|
2019-06-08 18:16:33 +02:00
|
|
|
int ws_echo_server_main(int port, bool greetings, const std::string& hostname)
|
2019-02-23 06:13:56 +01:00
|
|
|
{
|
2019-03-14 07:09:45 +01:00
|
|
|
std::cout << "Listening on " << hostname << ":" << port << std::endl;
|
2019-02-23 06:13:56 +01:00
|
|
|
|
2019-03-14 07:09:45 +01:00
|
|
|
ix::WebSocketServer server(port, hostname);
|
2019-02-23 06:13:56 +01:00
|
|
|
|
|
|
|
server.setOnConnectionCallback(
|
2019-06-08 18:16:33 +02:00
|
|
|
[greetings](std::shared_ptr<ix::WebSocket> webSocket,
|
2019-09-23 19:25:23 +02:00
|
|
|
std::shared_ptr<ConnectionState> connectionState) {
|
2019-02-23 06:13:56 +01:00
|
|
|
webSocket->setOnMessageCallback(
|
2019-09-23 19:25:23 +02:00
|
|
|
[webSocket, connectionState, greetings](const WebSocketMessagePtr& msg) {
|
2019-06-09 20:33:17 +02:00
|
|
|
if (msg->type == ix::WebSocketMessageType::Open)
|
2019-02-23 06:13:56 +01:00
|
|
|
{
|
|
|
|
std::cerr << "New connection" << std::endl;
|
2019-03-21 02:34:24 +01:00
|
|
|
std::cerr << "id: " << connectionState->getId() << std::endl;
|
2019-06-09 20:33:17 +02:00
|
|
|
std::cerr << "Uri: " << msg->openInfo.uri << std::endl;
|
2019-02-23 06:13:56 +01:00
|
|
|
std::cerr << "Headers:" << std::endl;
|
2019-06-09 20:33:17 +02:00
|
|
|
for (auto it : msg->openInfo.headers)
|
2019-02-23 06:13:56 +01:00
|
|
|
{
|
|
|
|
std::cerr << it.first << ": " << it.second << std::endl;
|
|
|
|
}
|
2019-06-08 18:16:33 +02:00
|
|
|
|
|
|
|
if (greetings)
|
|
|
|
{
|
|
|
|
webSocket->sendText("Welcome !");
|
|
|
|
}
|
2019-02-23 06:13:56 +01:00
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Close)
|
2019-02-23 06:13:56 +01:00
|
|
|
{
|
2019-03-04 22:56:30 +01:00
|
|
|
std::cerr << "Closed connection"
|
2019-09-23 19:25:23 +02:00
|
|
|
<< " code " << msg->closeInfo.code << " reason "
|
|
|
|
<< msg->closeInfo.reason << std::endl;
|
2019-03-04 22:56:30 +01:00
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Error)
|
2019-03-04 22:56:30 +01:00
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2019-09-23 19:25:23 +02:00
|
|
|
ss << "Connection error: " << msg->errorInfo.reason << std::endl;
|
|
|
|
ss << "#retries: " << msg->errorInfo.retries << std::endl;
|
|
|
|
ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl;
|
|
|
|
ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl;
|
2019-03-04 22:56:30 +01:00
|
|
|
std::cerr << ss.str();
|
2019-02-23 06:13:56 +01:00
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Message)
|
2019-02-23 06:13:56 +01:00
|
|
|
{
|
2019-09-23 19:25:23 +02:00
|
|
|
std::cerr << "Received " << msg->wireSize << " bytes" << std::endl;
|
2019-06-09 20:55:34 +02:00
|
|
|
webSocket->send(msg->str, msg->binary);
|
2019-02-23 06:13:56 +01:00
|
|
|
}
|
2019-09-23 19:25:23 +02:00
|
|
|
});
|
|
|
|
});
|
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;
|
|
|
|
}
|
2019-09-23 19:25:23 +02:00
|
|
|
} // namespace ix
|