|
| 1 | +/* |
| 2 | + * ws_push_server.cpp |
| 3 | + * Author: Benjamin Sergeant |
| 4 | + * Copyright (c) 2020 Machine Zone, Inc. All rights reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <ixwebsocket/IXNetSystem.h> |
| 8 | +#include <ixwebsocket/IXWebSocketServer.h> |
| 9 | +#include <spdlog/spdlog.h> |
| 10 | +#include <sstream> |
| 11 | + |
| 12 | +namespace ix |
| 13 | +{ |
| 14 | + int ws_push_server(int port, |
| 15 | + bool greetings, |
| 16 | + const std::string& hostname, |
| 17 | + const ix::SocketTLSOptions& tlsOptions, |
| 18 | + bool ipv6, |
| 19 | + bool disablePerMessageDeflate, |
| 20 | + bool disablePong, |
| 21 | + const std::string& sendMsg) |
| 22 | + { |
| 23 | + spdlog::info("Listening on {}:{}", hostname, port); |
| 24 | + |
| 25 | + ix::WebSocketServer server(port, |
| 26 | + hostname, |
| 27 | + SocketServer::kDefaultTcpBacklog, |
| 28 | + SocketServer::kDefaultMaxConnections, |
| 29 | + WebSocketServer::kDefaultHandShakeTimeoutSecs, |
| 30 | + (ipv6) ? AF_INET6 : AF_INET); |
| 31 | + |
| 32 | + server.setTLSOptions(tlsOptions); |
| 33 | + |
| 34 | + if (disablePerMessageDeflate) |
| 35 | + { |
| 36 | + spdlog::info("Disable per message deflate"); |
| 37 | + server.disablePerMessageDeflate(); |
| 38 | + } |
| 39 | + |
| 40 | + if (disablePong) |
| 41 | + { |
| 42 | + spdlog::info("Disable responding to PING messages with PONG"); |
| 43 | + server.disablePong(); |
| 44 | + } |
| 45 | + |
| 46 | + server.setOnClientMessageCallback( |
| 47 | + [greetings, &sendMsg](std::shared_ptr<ConnectionState> connectionState, |
| 48 | + ConnectionInfo& connectionInfo, |
| 49 | + WebSocket& webSocket, |
| 50 | + const WebSocketMessagePtr& msg) { |
| 51 | + auto remoteIp = connectionInfo.remoteIp; |
| 52 | + if (msg->type == ix::WebSocketMessageType::Open) |
| 53 | + { |
| 54 | + spdlog::info("New connection"); |
| 55 | + spdlog::info("remote ip: {}", remoteIp); |
| 56 | + spdlog::info("id: {}", connectionState->getId()); |
| 57 | + spdlog::info("Uri: {}", msg->openInfo.uri); |
| 58 | + spdlog::info("Headers:"); |
| 59 | + for (auto it : msg->openInfo.headers) |
| 60 | + { |
| 61 | + spdlog::info("{}: {}", it.first, it.second); |
| 62 | + } |
| 63 | + |
| 64 | + if (greetings) |
| 65 | + { |
| 66 | + webSocket.sendText("Welcome !"); |
| 67 | + } |
| 68 | + |
| 69 | + bool binary = false; |
| 70 | + while (true) |
| 71 | + { |
| 72 | + webSocket.send(sendMsg, binary); |
| 73 | + } |
| 74 | + } |
| 75 | + else if (msg->type == ix::WebSocketMessageType::Close) |
| 76 | + { |
| 77 | + spdlog::info("Closed connection: client id {} code {} reason {}", |
| 78 | + connectionState->getId(), |
| 79 | + msg->closeInfo.code, |
| 80 | + msg->closeInfo.reason); |
| 81 | + } |
| 82 | + else if (msg->type == ix::WebSocketMessageType::Error) |
| 83 | + { |
| 84 | + spdlog::error("Connection error: {}", msg->errorInfo.reason); |
| 85 | + spdlog::error("#retries: {}", msg->errorInfo.retries); |
| 86 | + spdlog::error("Wait time(ms): {}", msg->errorInfo.wait_time); |
| 87 | + spdlog::error("HTTP Status: {}", msg->errorInfo.http_status); |
| 88 | + } |
| 89 | + else if (msg->type == ix::WebSocketMessageType::Message) |
| 90 | + { |
| 91 | + spdlog::info("Received {} bytes", msg->wireSize); |
| 92 | + webSocket.send(msg->str, msg->binary); |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + auto res = server.listen(); |
| 97 | + if (!res.first) |
| 98 | + { |
| 99 | + spdlog::error(res.second); |
| 100 | + return 1; |
| 101 | + } |
| 102 | + |
| 103 | + server.start(); |
| 104 | + server.wait(); |
| 105 | + |
| 106 | + return 0; |
| 107 | + } |
| 108 | +} // namespace ix |
0 commit comments