@@ -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