ping pong added to ws
This commit is contained in:
parent
d642ef1a89
commit
e8583000b8
2
examples/ping_pong/.gitignore
vendored
2
examples/ping_pong/.gitignore
vendored
@ -1,2 +0,0 @@
|
||||
venv
|
||||
build
|
@ -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)
|
@ -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
|
@ -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'))
|
@ -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()
|
@ -1,9 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
test -d build || {
|
||||
mkdir -p build
|
||||
cd build
|
||||
cmake ..
|
||||
}
|
||||
(cd build ; make)
|
||||
./build/ping_pong ws://localhost:5678
|
@ -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
|
||||
|
@ -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 \
|
||||
|
14
ws/ws.cpp
14
ws/ws.cpp
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
/*
|
||||
* ping_pong.cpp
|
||||
* ws_ping_pong.cpp
|
||||
* 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>
|
||||
@ -9,15 +9,8 @@
|
||||
#include <ixwebsocket/IXWebSocket.h>
|
||||
#include <ixwebsocket/IXSocket.h>
|
||||
|
||||
using namespace ix;
|
||||
|
||||
namespace
|
||||
namespace ix
|
||||
{
|
||||
void log(const std::string& msg)
|
||||
{
|
||||
std::cout << msg << std::endl;
|
||||
}
|
||||
|
||||
class WebSocketPingPong
|
||||
{
|
||||
public:
|
||||
@ -33,6 +26,8 @@ namespace
|
||||
private:
|
||||
std::string _url;
|
||||
ix::WebSocket _webSocket;
|
||||
|
||||
void log(const std::string& msg);
|
||||
};
|
||||
|
||||
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()
|
||||
{
|
||||
_webSocket.stop();
|
||||
@ -124,7 +124,7 @@ namespace
|
||||
_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;
|
||||
WebSocketPingPong webSocketPingPong(url);
|
||||
@ -155,17 +155,3 @@ namespace
|
||||
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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user