2019-02-23 06:13:56 +01:00
|
|
|
/*
|
2019-11-15 23:28:30 +01:00
|
|
|
* ws_echo_server.cpp
|
2019-02-23 06:13:56 +01:00
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2020-01-27 01:44:44 +01:00
|
|
|
#include <ixwebsocket/IXNetSystem.h>
|
2020-02-19 06:38:28 +01:00
|
|
|
#include <ixwebsocket/IXWebSocketServer.h>
|
2019-12-25 06:55:34 +01:00
|
|
|
#include <spdlog/spdlog.h>
|
2019-09-23 19:25:23 +02:00
|
|
|
#include <sstream>
|
2019-02-23 06:13:56 +01:00
|
|
|
|
|
|
|
namespace ix
|
|
|
|
{
|
2019-09-30 03:29:51 +02:00
|
|
|
int ws_echo_server_main(int port,
|
|
|
|
bool greetings,
|
|
|
|
const std::string& hostname,
|
2020-01-27 01:44:44 +01:00
|
|
|
const ix::SocketTLSOptions& tlsOptions,
|
2020-02-19 06:38:28 +01:00
|
|
|
bool ipv6,
|
2020-03-18 08:01:57 +01:00
|
|
|
bool disablePerMessageDeflate,
|
|
|
|
bool disablePong)
|
2019-02-23 06:13:56 +01:00
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info("Listening on {}:{}", hostname, port);
|
2019-02-23 06:13:56 +01:00
|
|
|
|
2020-01-27 01:44:44 +01:00
|
|
|
ix::WebSocketServer server(port,
|
|
|
|
hostname,
|
|
|
|
SocketServer::kDefaultTcpBacklog,
|
|
|
|
SocketServer::kDefaultMaxConnections,
|
|
|
|
WebSocketServer::kDefaultHandShakeTimeoutSecs,
|
|
|
|
(ipv6) ? AF_INET6 : AF_INET);
|
|
|
|
|
2019-10-01 03:21:20 +02:00
|
|
|
server.setTLSOptions(tlsOptions);
|
2019-02-23 06:13:56 +01:00
|
|
|
|
2020-02-19 06:38:28 +01:00
|
|
|
if (disablePerMessageDeflate)
|
|
|
|
{
|
|
|
|
spdlog::info("Disable per message deflate");
|
|
|
|
server.disablePerMessageDeflate();
|
|
|
|
}
|
|
|
|
|
2020-03-18 08:01:57 +01:00
|
|
|
if (disablePong)
|
|
|
|
{
|
|
|
|
spdlog::info("Disable responding to PING messages with PONG");
|
|
|
|
server.disablePong();
|
|
|
|
}
|
|
|
|
|
2020-07-24 04:29:41 +02:00
|
|
|
server.setOnClientMessageCallback(
|
|
|
|
[greetings](std::shared_ptr<ConnectionState> connectionState,
|
|
|
|
ConnectionInfo& connectionInfo,
|
|
|
|
WebSocket& webSocket,
|
|
|
|
const WebSocketMessagePtr& msg) {
|
|
|
|
auto remoteIp = connectionInfo.remoteIp;
|
|
|
|
if (msg->type == ix::WebSocketMessageType::Open)
|
|
|
|
{
|
|
|
|
spdlog::info("New connection");
|
|
|
|
spdlog::info("remote ip: {}", remoteIp);
|
|
|
|
spdlog::info("id: {}", connectionState->getId());
|
|
|
|
spdlog::info("Uri: {}", msg->openInfo.uri);
|
|
|
|
spdlog::info("Headers:");
|
|
|
|
for (auto it : msg->openInfo.headers)
|
|
|
|
{
|
|
|
|
spdlog::info("{}: {}", it.first, it.second);
|
|
|
|
}
|
2019-06-08 18:16:33 +02:00
|
|
|
|
2020-07-24 04:29:41 +02:00
|
|
|
if (greetings)
|
|
|
|
{
|
|
|
|
webSocket.sendText("Welcome !");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Close)
|
|
|
|
{
|
|
|
|
spdlog::info("Closed connection: client id {} code {} reason {}",
|
|
|
|
connectionState->getId(),
|
|
|
|
msg->closeInfo.code,
|
|
|
|
msg->closeInfo.reason);
|
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Error)
|
|
|
|
{
|
|
|
|
spdlog::error("Connection error: {}", msg->errorInfo.reason);
|
|
|
|
spdlog::error("#retries: {}", msg->errorInfo.retries);
|
|
|
|
spdlog::error("Wait time(ms): {}", msg->errorInfo.wait_time);
|
|
|
|
spdlog::error("HTTP Status: {}", msg->errorInfo.http_status);
|
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Message)
|
|
|
|
{
|
|
|
|
spdlog::info("Received {} bytes", msg->wireSize);
|
|
|
|
webSocket.send(msg->str, msg->binary);
|
|
|
|
}
|
2019-09-23 19:25:23 +02:00
|
|
|
});
|
2019-02-23 06:13:56 +01:00
|
|
|
|
|
|
|
auto res = server.listen();
|
|
|
|
if (!res.first)
|
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::error(res.second);
|
2019-02-23 06:13:56 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
server.start();
|
|
|
|
server.wait();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-09-23 19:25:23 +02:00
|
|
|
} // namespace ix
|