add echo and broadcast server as ws sub-commands
This commit is contained in:
parent
c4054d4984
commit
1c775cb759
9
examples/broadcast_server/.gitignore
vendored
9
examples/broadcast_server/.gitignore
vendored
@ -1,9 +0,0 @@
|
|||||||
CMakeCache.txt
|
|
||||||
package-lock.json
|
|
||||||
CMakeFiles
|
|
||||||
ixwebsocket_unittest
|
|
||||||
cmake_install.cmake
|
|
||||||
node_modules
|
|
||||||
ixwebsocket
|
|
||||||
Makefile
|
|
||||||
build
|
|
@ -1,28 +0,0 @@
|
|||||||
#
|
|
||||||
# Author: Benjamin Sergeant
|
|
||||||
# Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
|
||||||
#
|
|
||||||
|
|
||||||
cmake_minimum_required (VERSION 3.4.1)
|
|
||||||
project (broadcast_server)
|
|
||||||
|
|
||||||
# There's -Weverything too for clang
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Wshorten-64-to-32")
|
|
||||||
|
|
||||||
set (OPENSSL_PREFIX /usr/local/opt/openssl) # Homebrew openssl
|
|
||||||
|
|
||||||
set (CMAKE_CXX_STANDARD 14)
|
|
||||||
|
|
||||||
option(USE_TLS "Add TLS support" ON)
|
|
||||||
|
|
||||||
include_directories(broadcast_server .)
|
|
||||||
|
|
||||||
add_executable(broadcast_server
|
|
||||||
broadcast_server.cpp)
|
|
||||||
|
|
||||||
if (APPLE AND USE_TLS)
|
|
||||||
target_link_libraries(broadcast_server "-framework foundation" "-framework security")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
target_link_libraries(broadcast_server ixwebsocket)
|
|
||||||
install(TARGETS broadcast_server DESTINATION bin)
|
|
@ -1,75 +0,0 @@
|
|||||||
/*
|
|
||||||
* broadcast_server.cpp
|
|
||||||
* Author: Benjamin Sergeant
|
|
||||||
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <ixwebsocket/IXWebSocketServer.h>
|
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
|
||||||
{
|
|
||||||
int port = 8080;
|
|
||||||
if (argc == 2)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << argv[1];
|
|
||||||
ss >> port;
|
|
||||||
}
|
|
||||||
|
|
||||||
ix::WebSocketServer server(port);
|
|
||||||
|
|
||||||
server.setOnConnectionCallback(
|
|
||||||
[&server](std::shared_ptr<ix::WebSocket> webSocket)
|
|
||||||
{
|
|
||||||
webSocket->setOnMessageCallback(
|
|
||||||
[webSocket, &server](ix::WebSocketMessageType messageType,
|
|
||||||
const std::string& str,
|
|
||||||
size_t wireSize,
|
|
||||||
const ix::WebSocketErrorInfo& error,
|
|
||||||
const ix::WebSocketOpenInfo& openInfo,
|
|
||||||
const ix::WebSocketCloseInfo& closeInfo)
|
|
||||||
{
|
|
||||||
if (messageType == ix::WebSocket_MessageType_Open)
|
|
||||||
{
|
|
||||||
std::cerr << "New connection" << std::endl;
|
|
||||||
std::cerr << "Uri: " << openInfo.uri << std::endl;
|
|
||||||
std::cerr << "Headers:" << std::endl;
|
|
||||||
for (auto it : openInfo.headers)
|
|
||||||
{
|
|
||||||
std::cerr << it.first << ": " << it.second << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (messageType == ix::WebSocket_MessageType_Close)
|
|
||||||
{
|
|
||||||
std::cerr << "Closed connection" << std::endl;
|
|
||||||
}
|
|
||||||
else if (messageType == ix::WebSocket_MessageType_Message)
|
|
||||||
{
|
|
||||||
std::cerr << "Received " << wireSize << " bytes" << std::endl;
|
|
||||||
for (auto&& client : server.getClients())
|
|
||||||
{
|
|
||||||
if (client != webSocket)
|
|
||||||
{
|
|
||||||
client->send(str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
auto res = server.listen();
|
|
||||||
if (!res.first)
|
|
||||||
{
|
|
||||||
std::cerr << res.second << std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
server.start();
|
|
||||||
server.wait();
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
#
|
|
||||||
# Author: Benjamin Sergeant
|
|
||||||
# Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
|
||||||
#
|
|
||||||
|
|
||||||
cmake_minimum_required (VERSION 3.4.1)
|
|
||||||
project (echo_server)
|
|
||||||
|
|
||||||
# There's -Weverything too for clang
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Wshorten-64-to-32")
|
|
||||||
|
|
||||||
set (OPENSSL_PREFIX /usr/local/opt/openssl) # Homebrew openssl
|
|
||||||
|
|
||||||
set (CMAKE_CXX_STANDARD 14)
|
|
||||||
|
|
||||||
option(USE_TLS "Add TLS support" ON)
|
|
||||||
|
|
||||||
include_directories(echo_server .)
|
|
||||||
|
|
||||||
add_executable(echo_server
|
|
||||||
echo_server.cpp)
|
|
||||||
|
|
||||||
if (APPLE AND USE_TLS)
|
|
||||||
target_link_libraries(echo_server "-framework foundation" "-framework security")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
target_link_libraries(echo_server ixwebsocket)
|
|
||||||
install(TARGETS echo_server DESTINATION bin)
|
|
@ -1,68 +0,0 @@
|
|||||||
/*
|
|
||||||
* echo_server.cpp
|
|
||||||
* Author: Benjamin Sergeant
|
|
||||||
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <ixwebsocket/IXWebSocketServer.h>
|
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
|
||||||
{
|
|
||||||
int port = 8080;
|
|
||||||
if (argc == 2)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << argv[1];
|
|
||||||
ss >> port;
|
|
||||||
}
|
|
||||||
|
|
||||||
ix::WebSocketServer server(port);
|
|
||||||
|
|
||||||
server.setOnConnectionCallback(
|
|
||||||
[&server](std::shared_ptr<ix::WebSocket> webSocket)
|
|
||||||
{
|
|
||||||
webSocket->setOnMessageCallback(
|
|
||||||
[webSocket, &server](ix::WebSocketMessageType messageType,
|
|
||||||
const std::string& str,
|
|
||||||
size_t wireSize,
|
|
||||||
const ix::WebSocketErrorInfo& error,
|
|
||||||
const ix::WebSocketOpenInfo& openInfo,
|
|
||||||
const ix::WebSocketCloseInfo& closeInfo)
|
|
||||||
{
|
|
||||||
if (messageType == ix::WebSocket_MessageType_Open)
|
|
||||||
{
|
|
||||||
std::cerr << "New connection" << std::endl;
|
|
||||||
std::cerr << "Uri: " << openInfo.uri << std::endl;
|
|
||||||
std::cerr << "Headers:" << std::endl;
|
|
||||||
for (auto it : openInfo.headers)
|
|
||||||
{
|
|
||||||
std::cerr << it.first << ": " << it.second << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (messageType == ix::WebSocket_MessageType_Close)
|
|
||||||
{
|
|
||||||
std::cerr << "Closed connection" << std::endl;
|
|
||||||
}
|
|
||||||
else if (messageType == ix::WebSocket_MessageType_Message)
|
|
||||||
{
|
|
||||||
webSocket->send(str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
auto res = server.listen();
|
|
||||||
if (!res.first)
|
|
||||||
{
|
|
||||||
std::cerr << res.second << std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
server.start();
|
|
||||||
server.wait();
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
2
makefile
2
makefile
@ -4,7 +4,7 @@
|
|||||||
all: brew
|
all: brew
|
||||||
|
|
||||||
brew:
|
brew:
|
||||||
mkdir -p build && (cd build ; cmake .. ; make)
|
mkdir -p build && (cd build ; cmake .. ; make -j install)
|
||||||
|
|
||||||
.PHONY: docker
|
.PHONY: docker
|
||||||
docker:
|
docker:
|
||||||
|
@ -7,7 +7,7 @@ cmake_minimum_required (VERSION 3.4.1)
|
|||||||
project (ws)
|
project (ws)
|
||||||
|
|
||||||
# There's -Weverything too for clang
|
# There's -Weverything too for clang
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Wshorten-64-to-32")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic")
|
||||||
|
|
||||||
set (CMAKE_CXX_STANDARD 14)
|
set (CMAKE_CXX_STANDARD 14)
|
||||||
|
|
||||||
@ -23,6 +23,8 @@ add_executable(ws
|
|||||||
ixcrypto/IXHash.cpp
|
ixcrypto/IXHash.cpp
|
||||||
ixcrypto/IXUuid.cpp
|
ixcrypto/IXUuid.cpp
|
||||||
|
|
||||||
|
ws_broadcast_server.cpp
|
||||||
|
ws_echo_server.cpp
|
||||||
ws_chat.cpp
|
ws_chat.cpp
|
||||||
ws_connect.cpp
|
ws_connect.cpp
|
||||||
ws_transfer.cpp
|
ws_transfer.cpp
|
||||||
|
@ -28,6 +28,8 @@ g++ --std=c++14 \
|
|||||||
ixcrypto/IXBase64.cpp \
|
ixcrypto/IXBase64.cpp \
|
||||||
ixcrypto/IXHash.cpp \
|
ixcrypto/IXHash.cpp \
|
||||||
ixcrypto/IXUuid.cpp \
|
ixcrypto/IXUuid.cpp \
|
||||||
|
ws_broadcast_server.cpp \
|
||||||
|
ws_echo_server.cpp \
|
||||||
ws_chat.cpp \
|
ws_chat.cpp \
|
||||||
ws_connect.cpp \
|
ws_connect.cpp \
|
||||||
ws_transfer.cpp \
|
ws_transfer.cpp \
|
||||||
|
18
ws/ws.cpp
18
ws/ws.cpp
@ -16,6 +16,10 @@
|
|||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
|
int ws_echo_server_main(int port);
|
||||||
|
|
||||||
|
int ws_broadcast_server_main(int port);
|
||||||
|
|
||||||
int ws_chat_main(const std::string& url,
|
int ws_chat_main(const std::string& url,
|
||||||
const std::string& user);
|
const std::string& user);
|
||||||
|
|
||||||
@ -57,6 +61,12 @@ int main(int argc, char** argv)
|
|||||||
chatApp->add_option("url", url, "Connection url")->required();
|
chatApp->add_option("url", url, "Connection url")->required();
|
||||||
chatApp->add_option("user", user, "User name")->required();
|
chatApp->add_option("user", user, "User name")->required();
|
||||||
|
|
||||||
|
CLI::App* echoServerApp = app.add_subcommand("echo_server", "Echo server");
|
||||||
|
echoServerApp->add_option("--port", port, "Connection url");
|
||||||
|
|
||||||
|
CLI::App* broadcastServerApp = app.add_subcommand("broadcast_server", "Broadcasting server");
|
||||||
|
broadcastServerApp->add_option("--port", port, "Connection url");
|
||||||
|
|
||||||
CLI11_PARSE(app, argc, argv);
|
CLI11_PARSE(app, argc, argv);
|
||||||
|
|
||||||
if (app.got_subcommand("transfer"))
|
if (app.got_subcommand("transfer"))
|
||||||
@ -80,6 +90,14 @@ int main(int argc, char** argv)
|
|||||||
{
|
{
|
||||||
return ix::ws_chat_main(url, user);
|
return ix::ws_chat_main(url, user);
|
||||||
}
|
}
|
||||||
|
else if (app.got_subcommand("echo_server"))
|
||||||
|
{
|
||||||
|
return ix::ws_echo_server_main(port);
|
||||||
|
}
|
||||||
|
else if (app.got_subcommand("broadcast_server"))
|
||||||
|
{
|
||||||
|
return ix::ws_broadcast_server_main(port);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
assert(false);
|
assert(false);
|
||||||
|
72
ws/ws_broadcast_server.cpp
Normal file
72
ws/ws_broadcast_server.cpp
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
/*
|
||||||
|
* ws_broadcast_server.cpp
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <ixwebsocket/IXWebSocketServer.h>
|
||||||
|
|
||||||
|
namespace ix
|
||||||
|
{
|
||||||
|
int ws_broadcast_server_main(int port)
|
||||||
|
{
|
||||||
|
std::cout << "Listening on port " << port << std::endl;
|
||||||
|
|
||||||
|
ix::WebSocketServer server(port);
|
||||||
|
|
||||||
|
server.setOnConnectionCallback(
|
||||||
|
[&server](std::shared_ptr<ix::WebSocket> webSocket)
|
||||||
|
{
|
||||||
|
webSocket->setOnMessageCallback(
|
||||||
|
[webSocket, &server](ix::WebSocketMessageType messageType,
|
||||||
|
const std::string& str,
|
||||||
|
size_t wireSize,
|
||||||
|
const ix::WebSocketErrorInfo& error,
|
||||||
|
const ix::WebSocketOpenInfo& openInfo,
|
||||||
|
const ix::WebSocketCloseInfo& closeInfo)
|
||||||
|
{
|
||||||
|
if (messageType == ix::WebSocket_MessageType_Open)
|
||||||
|
{
|
||||||
|
std::cerr << "New connection" << std::endl;
|
||||||
|
std::cerr << "Uri: " << openInfo.uri << std::endl;
|
||||||
|
std::cerr << "Headers:" << std::endl;
|
||||||
|
for (auto it : openInfo.headers)
|
||||||
|
{
|
||||||
|
std::cerr << it.first << ": " << it.second << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (messageType == ix::WebSocket_MessageType_Close)
|
||||||
|
{
|
||||||
|
std::cerr << "Closed connection" << std::endl;
|
||||||
|
}
|
||||||
|
else if (messageType == ix::WebSocket_MessageType_Message)
|
||||||
|
{
|
||||||
|
std::cerr << "Received " << wireSize << " bytes" << std::endl;
|
||||||
|
for (auto&& client : server.getClients())
|
||||||
|
{
|
||||||
|
if (client != webSocket)
|
||||||
|
{
|
||||||
|
client->send(str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
auto res = server.listen();
|
||||||
|
if (!res.first)
|
||||||
|
{
|
||||||
|
std::cerr << res.second << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
server.start();
|
||||||
|
server.wait();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
68
ws/ws_echo_server.cpp
Normal file
68
ws/ws_echo_server.cpp
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
* ws_broadcast_server.cpp
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <ixwebsocket/IXWebSocketServer.h>
|
||||||
|
|
||||||
|
namespace ix
|
||||||
|
{
|
||||||
|
int ws_echo_server_main(int port)
|
||||||
|
{
|
||||||
|
std::cout << "Listening on port " << port << std::endl;
|
||||||
|
|
||||||
|
ix::WebSocketServer server(port);
|
||||||
|
|
||||||
|
server.setOnConnectionCallback(
|
||||||
|
[&server](std::shared_ptr<ix::WebSocket> webSocket)
|
||||||
|
{
|
||||||
|
webSocket->setOnMessageCallback(
|
||||||
|
[webSocket, &server](ix::WebSocketMessageType messageType,
|
||||||
|
const std::string& str,
|
||||||
|
size_t wireSize,
|
||||||
|
const ix::WebSocketErrorInfo& error,
|
||||||
|
const ix::WebSocketOpenInfo& openInfo,
|
||||||
|
const ix::WebSocketCloseInfo& closeInfo)
|
||||||
|
{
|
||||||
|
if (messageType == ix::WebSocket_MessageType_Open)
|
||||||
|
{
|
||||||
|
std::cerr << "New connection" << std::endl;
|
||||||
|
std::cerr << "Uri: " << openInfo.uri << std::endl;
|
||||||
|
std::cerr << "Headers:" << std::endl;
|
||||||
|
for (auto it : openInfo.headers)
|
||||||
|
{
|
||||||
|
std::cerr << it.first << ": " << it.second << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (messageType == ix::WebSocket_MessageType_Close)
|
||||||
|
{
|
||||||
|
std::cerr << "Closed connection" << std::endl;
|
||||||
|
}
|
||||||
|
else if (messageType == ix::WebSocket_MessageType_Message)
|
||||||
|
{
|
||||||
|
std::cerr << "Received "
|
||||||
|
<< wireSize << " bytes"
|
||||||
|
<< std::endl;
|
||||||
|
webSocket->send(str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
auto res = server.listen();
|
||||||
|
if (!res.first)
|
||||||
|
{
|
||||||
|
std::cerr << res.second << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
server.start();
|
||||||
|
server.wait();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user