ping pong added to ws

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

View File

@ -1,2 +0,0 @@
venv
build

View File

@ -1,25 +0,0 @@
#
# Author: Benjamin Sergeant
# Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
#
cmake_minimum_required (VERSION 3.4.1)
project (ping_pong)
set (CMAKE_CXX_STANDARD 14)
option(USE_TLS "Add TLS support" ON)
add_executable(ping_pong ping_pong.cpp)
if (APPLE AND USE_TLS)
target_link_libraries(ping_pong "-framework foundation" "-framework security")
endif()
if (WIN32)
target_link_libraries(ping_pong wsock32 ws2_32)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif()
target_link_libraries(ping_pong ixwebsocket)
install(TARGETS ping_pong DESTINATION bin)

View File

@ -1,15 +0,0 @@
#!/bin/sh
#
# Author: Benjamin Sergeant
# Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
#
# 'manual' way of building. You can also use cmake.
g++ --std=c++11 \
../../ixwebsocket/IXSocket.cpp \
../../ixwebsocket/IXWebSocketTransport.cpp \
../../ixwebsocket/IXWebSocket.cpp \
-I ../.. \
cmd_websocket_chat.cpp \
-o cmd_websocket_chat

View File

@ -1,17 +0,0 @@
#!/usr/bin/env python
import asyncio
import websockets
async def hello(uri):
async with websockets.connect(uri) as websocket:
await websocket.send("Hello world!")
response = await websocket.recv()
print(response)
pong_waiter = await websocket.ping('coucou')
ret = await pong_waiter # only if you want to wait for the pong
print(ret)
asyncio.get_event_loop().run_until_complete(
hello('ws://localhost:5678'))

View File

@ -1,21 +0,0 @@
#!/usr/bin/env python
import os
import asyncio
import websockets
async def echo(websocket, path):
async for message in websocket:
print(message)
await websocket.send(message)
if os.getenv('TEST_CLOSE'):
print('Closing')
# breakpoint()
await websocket.close(1001, 'close message')
# await websocket.close()
break
asyncio.get_event_loop().run_until_complete(
websockets.serve(echo, 'localhost', 5678))
asyncio.get_event_loop().run_forever()

View File

@ -1,9 +0,0 @@
#!/bin/sh
test -d build || {
mkdir -p build
cd build
cmake ..
}
(cd build ; make)
./build/ping_pong ws://localhost:5678

View File

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

View File

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

View File

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

View File

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

View File

@ -1,7 +1,7 @@
/* /*
* ping_pong.cpp * ws_ping_pong.cpp
* Author: Benjamin Sergeant * Author: Benjamin Sergeant
* Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved. * Copyright (c) 2018-2019 Machine Zone, Inc. All rights reserved.
*/ */
#include <iostream> #include <iostream>
@ -9,15 +9,8 @@
#include <ixwebsocket/IXWebSocket.h> #include <ixwebsocket/IXWebSocket.h>
#include <ixwebsocket/IXSocket.h> #include <ixwebsocket/IXSocket.h>
using namespace ix; namespace ix
namespace
{ {
void log(const std::string& msg)
{
std::cout << msg << std::endl;
}
class WebSocketPingPong class WebSocketPingPong
{ {
public: public:
@ -33,6 +26,8 @@ namespace
private: private:
std::string _url; std::string _url;
ix::WebSocket _webSocket; ix::WebSocket _webSocket;
void log(const std::string& msg);
}; };
WebSocketPingPong::WebSocketPingPong(const std::string& url) : WebSocketPingPong::WebSocketPingPong(const std::string& url) :
@ -41,6 +36,11 @@ namespace
; ;
} }
void WebSocketPingPong::log(const std::string& msg)
{
std::cout << msg << std::endl;
}
void WebSocketPingPong::stop() void WebSocketPingPong::stop()
{ {
_webSocket.stop(); _webSocket.stop();
@ -124,7 +124,7 @@ namespace
_webSocket.send(text); _webSocket.send(text);
} }
void interactiveMain(const std::string& url) int ws_ping_pong_main(const std::string& url)
{ {
std::cout << "Type Ctrl-D to exit prompt..." << std::endl; std::cout << "Type Ctrl-D to exit prompt..." << std::endl;
WebSocketPingPong webSocketPingPong(url); WebSocketPingPong webSocketPingPong(url);
@ -155,17 +155,3 @@ namespace
webSocketPingPong.stop(); webSocketPingPong.stop();
} }
} }
int main(int argc, char** argv)
{
if (argc != 2)
{
std::cerr << "Usage: ping_pong <url>" << std::endl;
return 1;
}
std::string url = argv[1];
Socket::init();
interactiveMain(url);
return 0;
}