ping pong added to ws

This commit is contained in:
Benjamin Sergeant
2019-02-22 21:47:57 -08:00
parent 67e5957064
commit 98e7f5cb22
11 changed files with 29 additions and 128 deletions

View File

@ -23,6 +23,7 @@ add_executable(ws
ixcrypto/IXHash.cpp
ixcrypto/IXUuid.cpp
ws_ping_pong.cpp
ws_broadcast_server.cpp
ws_echo_server.cpp
ws_chat.cpp

View File

@ -28,6 +28,7 @@ g++ --std=c++14 \
ixcrypto/IXBase64.cpp \
ixcrypto/IXHash.cpp \
ixcrypto/IXUuid.cpp \
ws_ping_pong.cpp \
ws_broadcast_server.cpp \
ws_echo_server.cpp \
ws_chat.cpp \

View File

@ -13,9 +13,12 @@
#include <iostream>
#include <cli11/CLI11.hpp>
#include <ixwebsocket/IXSocket.h>
namespace ix
{
int ws_ping_pong_main(const std::string& url);
int ws_echo_server_main(int port);
int ws_broadcast_server_main(int port);
@ -39,7 +42,7 @@ int main(int argc, char** argv)
CLI::App app{"ws is a websocket tool"};
app.require_subcommand();
std::string url;
std::string url("ws://127.0.0.1:8080");
std::string path;
std::string user;
int port = 8080;
@ -67,8 +70,13 @@ int main(int argc, char** argv)
CLI::App* broadcastServerApp = app.add_subcommand("broadcast_server", "Broadcasting server");
broadcastServerApp->add_option("--port", port, "Connection url");
CLI::App* pingPongApp = app.add_subcommand("ping", "Ping pong");
pingPongApp->add_option("url", url, "Connection url")->required();
CLI11_PARSE(app, argc, argv);
ix::Socket::init();
if (app.got_subcommand("transfer"))
{
return ix::ws_transfer_main(port);
@ -98,6 +106,10 @@ int main(int argc, char** argv)
{
return ix::ws_broadcast_server_main(port);
}
else if (app.got_subcommand("ping"))
{
return ix::ws_ping_pong_main(url);
}
else
{
assert(false);

View File

@ -7,8 +7,7 @@
//
// Simple chat program that talks to the node.js server at
// websocket_chat_server/broacast-server.js
//
//
#include <iostream>
#include <sstream>
#include <queue>
@ -165,8 +164,8 @@ namespace ix
_webSocket.send(encodeMessage(text));
}
void interactiveMain(const std::string& url,
const std::string& user)
int ws_chat_main(const std::string& url,
const std::string& user)
{
std::cout << "Type Ctrl-D to exit prompt..." << std::endl;
WebSocketChat webSocketChat(url, user);
@ -189,13 +188,4 @@ namespace ix
std::cout << std::endl;
webSocketChat.stop();
}
int ws_chat_main(const std::string& url,
const std::string& user)
{
Socket::init();
interactiveMain(url, user);
return 0;
}
}

157
ws/ws_ping_pong.cpp Normal file
View File

@ -0,0 +1,157 @@
/*
* ws_ping_pong.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2018-2019 Machine Zone, Inc. All rights reserved.
*/
#include <iostream>
#include <sstream>
#include <ixwebsocket/IXWebSocket.h>
#include <ixwebsocket/IXSocket.h>
namespace ix
{
class WebSocketPingPong
{
public:
WebSocketPingPong(const std::string& _url);
void subscribe(const std::string& channel);
void start();
void stop();
void ping(const std::string& text);
void send(const std::string& text);
private:
std::string _url;
ix::WebSocket _webSocket;
void log(const std::string& msg);
};
WebSocketPingPong::WebSocketPingPong(const std::string& url) :
_url(url)
{
;
}
void WebSocketPingPong::log(const std::string& msg)
{
std::cout << msg << std::endl;
}
void WebSocketPingPong::stop()
{
_webSocket.stop();
}
void WebSocketPingPong::start()
{
_webSocket.setUrl(_url);
std::stringstream ss;
log(std::string("Connecting to url: ") + _url);
_webSocket.setOnMessageCallback(
[this](ix::WebSocketMessageType messageType,
const std::string& str,
size_t wireSize,
const ix::WebSocketErrorInfo& error,
const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo)
{
std::stringstream ss;
if (messageType == ix::WebSocket_MessageType_Open)
{
log("ping_pong: connected");
}
else if (messageType == ix::WebSocket_MessageType_Close)
{
ss << "ping_pong: disconnected:"
<< " code " << closeInfo.code
<< " reason " << closeInfo.reason
<< str;
log(ss.str());
}
else if (messageType == ix::WebSocket_MessageType_Message)
{
ss << "ping_pong: received message: "
<< str;
log(ss.str());
}
else if (messageType == ix::WebSocket_MessageType_Ping)
{
ss << "ping_pong: received ping message: "
<< str;
log(ss.str());
}
else if (messageType == ix::WebSocket_MessageType_Pong)
{
ss << "ping_pong: received pong message: "
<< str;
log(ss.str());
}
else if (messageType == ix::WebSocket_MessageType_Error)
{
ss << "Connection error: " << error.reason << std::endl;
ss << "#retries: " << error.retries << std::endl;
ss << "Wait time(ms): " << error.wait_time << std::endl;
ss << "HTTP Status: " << error.http_status << std::endl;
log(ss.str());
}
else
{
ss << "Invalid ix::WebSocketMessageType";
log(ss.str());
}
});
_webSocket.start();
}
void WebSocketPingPong::ping(const std::string& text)
{
if (!_webSocket.ping(text).success)
{
std::cerr << "Failed to send ping message. Message too long (> 125 bytes) or endpoint is disconnected"
<< std::endl;
}
}
void WebSocketPingPong::send(const std::string& text)
{
_webSocket.send(text);
}
int ws_ping_pong_main(const std::string& url)
{
std::cout << "Type Ctrl-D to exit prompt..." << std::endl;
WebSocketPingPong webSocketPingPong(url);
webSocketPingPong.start();
while (true)
{
std::string text;
std::cout << "> " << std::flush;
std::getline(std::cin, text);
if (!std::cin)
{
break;
}
if (text == "/close")
{
webSocketPingPong.send(text);
}
else
{
webSocketPingPong.ping(text);
}
}
std::cout << std::endl;
webSocketPingPong.stop();
}
}