Skip to content

Commit d0359a1

Browse files
committed
new makeBroadcastServer websocket server method for classic servers, used by ws
1 parent 8910ebc commit d0359a1

File tree

3 files changed

+50
-93
lines changed

3 files changed

+50
-93
lines changed

ixwebsocket/IXWebSocketServer.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,45 @@ namespace ix
168168
std::lock_guard<std::mutex> lock(_clientsMutex);
169169
return _clients.size();
170170
}
171+
172+
//
173+
// Classic servers
174+
//
175+
void WebSocketServer::makeBroadcastServer()
176+
{
177+
setOnClientMessageCallback(
178+
[this](std::shared_ptr<ConnectionState> connectionState,
179+
WebSocket& webSocket,
180+
const WebSocketMessagePtr& msg) {
181+
auto remoteIp = connectionState->getRemoteIp();
182+
if (msg->type == ix::WebSocketMessageType::Message)
183+
{
184+
for (auto&& client : getClients())
185+
{
186+
if (client.get() != &webSocket)
187+
{
188+
client->send(msg->str, msg->binary);
189+
190+
do
191+
{
192+
size_t bufferedAmount = client->bufferedAmount();
193+
std::chrono::duration<double, std::milli> duration(500);
194+
std::this_thread::sleep_for(duration);
195+
} while (client->bufferedAmount() != 0);
196+
}
197+
}
198+
}
199+
});
200+
}
201+
202+
int WebSocketServer::listenAndStart()
203+
{
204+
auto res = listen();
205+
if (!res.first)
206+
{
207+
return 1;
208+
}
209+
210+
start();
211+
}
171212
} // namespace ix

ixwebsocket/IXWebSocketServer.h

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ namespace ix
4747
// Get all the connected clients
4848
std::set<std::shared_ptr<WebSocket>> getClients();
4949

50+
void makeBroadcastServer();
51+
int listenAndStart();
52+
5053
const static int kDefaultHandShakeTimeoutSecs;
5154

5255
private:

ws/ws.cpp

+6-93
Original file line numberDiff line numberDiff line change
@@ -439,93 +439,6 @@ namespace ix
439439
return generateReport(url) ? 0 : 1;
440440
}
441441

442-
//
443-
// broadcast server
444-
//
445-
int ws_broadcast_server_main(int port,
446-
const std::string& hostname,
447-
const ix::SocketTLSOptions& tlsOptions)
448-
{
449-
spdlog::info("Listening on {}:{}", hostname, port);
450-
451-
ix::WebSocketServer server(port, hostname);
452-
server.setTLSOptions(tlsOptions);
453-
454-
server.setOnClientMessageCallback(
455-
[&server](std::shared_ptr<ConnectionState> connectionState,
456-
WebSocket& webSocket,
457-
const WebSocketMessagePtr& msg) {
458-
auto remoteIp = connectionState->getRemoteIp();
459-
if (msg->type == ix::WebSocketMessageType::Open)
460-
{
461-
spdlog::info("New connection");
462-
spdlog::info("remote ip: {}", remoteIp);
463-
spdlog::info("id: {}", connectionState->getId());
464-
spdlog::info("Uri: {}", msg->openInfo.uri);
465-
spdlog::info("Headers:");
466-
for (auto it : msg->openInfo.headers)
467-
{
468-
spdlog::info("{}: {}", it.first, it.second);
469-
}
470-
}
471-
else if (msg->type == ix::WebSocketMessageType::Close)
472-
{
473-
spdlog::info("Closed connection: code {} reason {}",
474-
msg->closeInfo.code,
475-
msg->closeInfo.reason);
476-
}
477-
else if (msg->type == ix::WebSocketMessageType::Error)
478-
{
479-
std::stringstream ss;
480-
ss << "Connection error: " << msg->errorInfo.reason << std::endl;
481-
ss << "#retries: " << msg->errorInfo.retries << std::endl;
482-
ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl;
483-
ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl;
484-
spdlog::info(ss.str());
485-
}
486-
else if (msg->type == ix::WebSocketMessageType::Fragment)
487-
{
488-
spdlog::info("Received message fragment");
489-
}
490-
else if (msg->type == ix::WebSocketMessageType::Message)
491-
{
492-
spdlog::info("Received {} bytes", msg->wireSize);
493-
494-
for (auto&& client : server.getClients())
495-
{
496-
if (client.get() != &webSocket)
497-
{
498-
client->send(msg->str, msg->binary, [](int current, int total) -> bool {
499-
spdlog::info("Step {} out of {}", current, total);
500-
return true;
501-
});
502-
503-
do
504-
{
505-
size_t bufferedAmount = client->bufferedAmount();
506-
spdlog::info("{} bytes left to be sent", bufferedAmount);
507-
508-
std::chrono::duration<double, std::milli> duration(500);
509-
std::this_thread::sleep_for(duration);
510-
} while (client->bufferedAmount() != 0);
511-
}
512-
}
513-
}
514-
});
515-
516-
auto res = server.listen();
517-
if (!res.first)
518-
{
519-
spdlog::info(res.second);
520-
return 1;
521-
}
522-
523-
server.start();
524-
server.wait();
525-
526-
return 0;
527-
}
528-
529442
/*
530443
* ws_chat.cpp
531444
* Author: Benjamin Sergeant
@@ -2853,9 +2766,13 @@ int main(int argc, char** argv)
28532766
ret = ix::ws_push_server(
28542767
port, hostname, tlsOptions, ipv6, disablePerMessageDeflate, disablePong, sendMsg);
28552768
}
2856-
else if (app.got_subcommand("transfer"))
2769+
else if (app.got_subcommand("transfer") || app.got_subcommand("broadcast_server"))
28572770
{
2858-
ret = ix::ws_transfer_main(port, hostname, tlsOptions);
2771+
ix::WebSocketServer server(port, hostname);
2772+
server.setTLSOptions(tlsOptions);
2773+
server.makeBroadcastServer();
2774+
server.listenAndStart();
2775+
server.wait();
28592776
}
28602777
else if (app.got_subcommand("send"))
28612778
{
@@ -2870,10 +2787,6 @@ int main(int argc, char** argv)
28702787
{
28712788
ret = ix::ws_chat_main(url, user);
28722789
}
2873-
else if (app.got_subcommand("broadcast_server"))
2874-
{
2875-
ret = ix::ws_broadcast_server_main(port, hostname, tlsOptions);
2876-
}
28772790
else if (app.got_subcommand("ping"))
28782791
{
28792792
ret = ix::ws_ping_pong_main(url, tlsOptions);

0 commit comments

Comments
 (0)