Better ping/pong support
This commit is contained in:
1
examples/ping_pong/.gitignore
vendored
Normal file
1
examples/ping_pong/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
venv
|
27
examples/ping_pong/CMakeLists.txt
Normal file
27
examples/ping_pong/CMakeLists.txt
Normal file
@ -0,0 +1,27 @@
|
||||
#
|
||||
# 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 11)
|
||||
|
||||
option(USE_TLS "Add TLS support" ON)
|
||||
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket)
|
||||
|
||||
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)
|
17
examples/ping_pong/client.py
Normal file
17
examples/ping_pong/client.py
Normal file
@ -0,0 +1,17 @@
|
||||
#!/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'))
|
133
examples/ping_pong/ping_pong.cpp
Normal file
133
examples/ping_pong/ping_pong.cpp
Normal file
@ -0,0 +1,133 @@
|
||||
/*
|
||||
* ws_connect.cpp
|
||||
* Author: Benjamin Sergeant
|
||||
* Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <ixwebsocket/IXWebSocket.h>
|
||||
#include <ixwebsocket/IXSocket.h>
|
||||
|
||||
using namespace ix;
|
||||
|
||||
namespace
|
||||
{
|
||||
void log(const std::string& msg)
|
||||
{
|
||||
std::cout << msg << std::endl;
|
||||
}
|
||||
|
||||
class WebSocketPingPong
|
||||
{
|
||||
public:
|
||||
WebSocketPingPong(const std::string& _url);
|
||||
|
||||
void subscribe(const std::string& channel);
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
void ping(const std::string& text);
|
||||
|
||||
private:
|
||||
std::string _url;
|
||||
ix::WebSocket _webSocket;
|
||||
};
|
||||
|
||||
WebSocketPingPong::WebSocketPingPong(const std::string& url) :
|
||||
_url(url)
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
void WebSocketPingPong::stop()
|
||||
{
|
||||
_webSocket.stop();
|
||||
}
|
||||
|
||||
void WebSocketPingPong::start()
|
||||
{
|
||||
_webSocket.configure(_url);
|
||||
|
||||
std::stringstream ss;
|
||||
log(std::string("Connecting to url: ") + _url);
|
||||
|
||||
_webSocket.setOnMessageCallback(
|
||||
[this](ix::WebSocketMessageType messageType, const std::string& str, ix::WebSocketErrorInfo error)
|
||||
{
|
||||
std::stringstream ss;
|
||||
if (messageType == ix::WebSocket_MessageType_Open)
|
||||
{
|
||||
log("ws_connect: connected");
|
||||
}
|
||||
else if (messageType == ix::WebSocket_MessageType_Close)
|
||||
{
|
||||
log("ws_connect: disconnected");
|
||||
}
|
||||
else if (messageType == ix::WebSocket_MessageType_Message)
|
||||
{
|
||||
ss << "ws_connect: received 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 if (messageType == ix::WebSocket_MessageType_Pong)
|
||||
{
|
||||
ss << "Invalid ix::WebSocketMessageType";
|
||||
log(ss.str());
|
||||
}
|
||||
});
|
||||
|
||||
_webSocket.start();
|
||||
}
|
||||
|
||||
void WebSocketPingPong::ping(const std::string& text)
|
||||
{
|
||||
_webSocket.ping(text);
|
||||
}
|
||||
|
||||
void interactiveMain(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;
|
||||
}
|
||||
|
||||
webSocketPingPong.ping(text);
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
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;
|
||||
}
|
13
examples/ping_pong/server.py
Normal file
13
examples/ping_pong/server.py
Normal file
@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import asyncio
|
||||
import websockets
|
||||
|
||||
async def echo(websocket, path):
|
||||
async for message in websocket:
|
||||
print(message)
|
||||
await websocket.send(message)
|
||||
|
||||
asyncio.get_event_loop().run_until_complete(
|
||||
websockets.serve(echo, 'localhost', 5678))
|
||||
asyncio.get_event_loop().run_forever()
|
4
examples/ping_pong/test.sh
Normal file
4
examples/ping_pong/test.sh
Normal file
@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
|
||||
(cd build ; make)
|
||||
./build/ping_pong ws://localhost:5678
|
Reference in New Issue
Block a user