Skip to content

Commit f2e666e

Browse files
committedJun 9, 2019
client-mbedtls example added
1 parent c699024 commit f2e666e

File tree

8 files changed

+278
-3
lines changed

8 files changed

+278
-3
lines changed
 

‎.gitignore

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333

3434
# Idea
3535

36-
.idea/
36+
**/.idea/
37+
**/cmake-build-debug/
38+
**/build/
3739

38-
async-server/cmake-build-debug/
39-
server/cmake-build-debug/
4040

‎client-mbedtls/CMakeLists.txt

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
cmake_minimum_required(VERSION 3.1)
2+
3+
set(project_name websocket-client-mbedtls) ## rename your project here
4+
5+
project(${project_name})
6+
7+
set(CMAKE_CXX_STANDARD 11)
8+
9+
include_directories(src)
10+
11+
add_library(${project_name}-lib
12+
src/WSListener.cpp
13+
src/WSListener.hpp
14+
)
15+
16+
## link libs
17+
18+
find_package(oatpp 0.19.4 REQUIRED)
19+
find_package(oatpp-websocket 0.19.4 REQUIRED)
20+
find_package(oatpp-mbedtls 0.19.4 REQUIRED)
21+
22+
target_link_libraries(${project_name}-lib
23+
PUBLIC oatpp::oatpp
24+
PUBLIC oatpp::oatpp-test
25+
PUBLIC oatpp::oatpp-websocket
26+
PUBLIC oatpp::oatpp-mbedtls
27+
)
28+
29+
#################################################################
30+
## link mbedtls
31+
32+
link_directories(
33+
/usr/local/lib/
34+
)
35+
36+
#################################################################
37+
38+
## add executables
39+
40+
add_executable(${project_name}-exe
41+
src/App.cpp
42+
)
43+
target_link_libraries(${project_name}-exe ${project_name}-lib)
44+
add_dependencies(${project_name}-exe ${project_name}-lib)
45+
46+
add_executable(${project_name}-test
47+
test/tests.cpp
48+
test/WSTest.cpp
49+
test/WSTest.hpp
50+
)
51+
target_link_libraries(${project_name}-test ${project_name}-lib)
52+
add_dependencies(${project_name}-test ${project_name}-lib)
53+
54+
set_target_properties(${project_name}-lib ${project_name}-exe ${project_name}-test PROPERTIES
55+
CXX_STANDARD 11
56+
CXX_EXTENSIONS OFF
57+
CXX_STANDARD_REQUIRED ON
58+
LINKER_LANGUAGE CXX
59+
)

‎client-mbedtls/src/App.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
#include "WSListener.hpp"
3+
4+
#include "oatpp-websocket/WebSocket.hpp"
5+
#include "oatpp-websocket/Connector.hpp"
6+
7+
#include "oatpp-mbedtls/client/ConnectionProvider.hpp"
8+
#include "oatpp-mbedtls/Config.hpp"
9+
10+
#include <thread>
11+
12+
namespace {
13+
14+
bool finished = false;
15+
16+
void socketTask(const std::shared_ptr<oatpp::websocket::WebSocket>& websocket) {
17+
websocket->listen();
18+
OATPP_LOGD("websocket-client", "SOCKET CLOSED!!!");
19+
finished = true;
20+
}
21+
22+
}
23+
24+
void run() {
25+
26+
OATPP_LOGI("websocket-client", "Application Started");
27+
28+
auto config = oatpp::mbedtls::Config::createDefaultClientConfigShared();
29+
30+
auto connectionProvider = oatpp::mbedtls::client::ConnectionProvider::createShared(config, "echo.websocket.org", 443);
31+
32+
auto connector = oatpp::websocket::Connector::createShared(connectionProvider);
33+
34+
auto connection = connector->connect("/");
35+
36+
OATPP_LOGI("websocket-client", "Connected");
37+
38+
auto socket = oatpp::websocket::WebSocket::createShared(connection, true /* maskOutgoingMessages must be true for clients */);
39+
40+
std::mutex socketWriteMutex;
41+
42+
socket->setListener(std::make_shared<WSListener>(socketWriteMutex));
43+
44+
std::thread thread(socketTask, socket);
45+
46+
while(!finished) {
47+
{
48+
OATPP_LOGD("websocket-client", "sending message...");
49+
std::lock_guard<std::mutex> lock(socketWriteMutex);
50+
socket->sendOneFrameText("hello");
51+
}
52+
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
53+
}
54+
55+
thread.join();
56+
57+
}
58+
59+
int main() {
60+
oatpp::base::Environment::init();
61+
run();
62+
oatpp::base::Environment::destroy();
63+
return 0;
64+
}

‎client-mbedtls/src/WSListener.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// Created by Leonid on 2019-06-07.
3+
//
4+
5+
#include "WSListener.hpp"
6+
7+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
8+
// WSListener
9+
10+
void WSListener::onPing(const WebSocket& socket, const oatpp::String& message) {
11+
OATPP_LOGD(TAG, "onPing");
12+
std::lock_guard<std::mutex> lock(m_writeMutex);
13+
socket.sendPong(message);
14+
}
15+
16+
void WSListener::onPong(const WebSocket& socket, const oatpp::String& message) {
17+
OATPP_LOGD(TAG, "onPong");
18+
}
19+
20+
void WSListener::onClose(const WebSocket& socket, v_word16 code, const oatpp::String& message) {
21+
OATPP_LOGD(TAG, "onClose code=%d", code);
22+
}
23+
24+
void WSListener::readMessage(const WebSocket& socket, p_char8 data, oatpp::data::v_io_size size) {
25+
26+
if(size == 0) { // message transfer finished
27+
28+
auto wholeMessage = m_messageBuffer.toString();
29+
m_messageBuffer.clear();
30+
31+
OATPP_LOGD(TAG, "on message received '%s'", wholeMessage->c_str());
32+
33+
/* Send message in reply */
34+
//std::lock_guard<std::mutex> lock(m_writeMutex);
35+
//socket.sendOneFrameText( "Hello from oatpp!: " + wholeMessage);
36+
37+
} else if(size > 0) { // message frame received
38+
m_messageBuffer.write(data, size);
39+
}
40+
41+
}

‎client-mbedtls/src/WSListener.hpp

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// Created by Leonid on 2019-06-07.
3+
//
4+
5+
#ifndef WSListener_hpp
6+
#define WSListener_hpp
7+
8+
#include "oatpp-websocket/ConnectionHandler.hpp"
9+
#include "oatpp-websocket/WebSocket.hpp"
10+
11+
/**
12+
* WebSocket listener listens on incoming WebSocket events.
13+
*/
14+
class WSListener : public oatpp::websocket::WebSocket::Listener {
15+
private:
16+
static constexpr const char* TAG = "Client_WSListener";
17+
private:
18+
std::mutex& m_writeMutex;
19+
/**
20+
* Buffer for messages. Needed for multi-frame messages.
21+
*/
22+
oatpp::data::stream::ChunkedBuffer m_messageBuffer;
23+
public:
24+
25+
WSListener(std::mutex& writeMutex)
26+
: m_writeMutex(writeMutex)
27+
{}
28+
29+
/**
30+
* Called on "ping" frame.
31+
*/
32+
void onPing(const WebSocket& socket, const oatpp::String& message) override;
33+
34+
/**
35+
* Called on "pong" frame
36+
*/
37+
void onPong(const WebSocket& socket, const oatpp::String& message) override;
38+
39+
/**
40+
* Called on "close" frame
41+
*/
42+
void onClose(const WebSocket& socket, v_word16 code, const oatpp::String& message) override;
43+
44+
/**
45+
* Called on each message frame. After the last message will be called once-again with size == 0 to designate end of the message.
46+
*/
47+
void readMessage(const WebSocket& socket, p_char8 data, oatpp::data::v_io_size size) override;
48+
49+
};
50+
51+
#endif // WSListener_hpp

‎client-mbedtls/test/WSTest.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//
2+
// Created by Leonid on 2019-03-25.
3+
//
4+
5+
#include "WSTest.hpp"
6+
7+
void WSTest::onRun() {
8+
9+
OATPP_LOGD(TAG, "TODO - write tests");
10+
11+
}

‎client-mbedtls/test/WSTest.hpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// Created by Leonid on 2019-03-25.
3+
//
4+
5+
#ifndef MY_PROJECT_WEBSOCKETTEST_HPP
6+
#define MY_PROJECT_WEBSOCKETTEST_HPP
7+
8+
9+
#include "oatpp-test/UnitTest.hpp"
10+
11+
class WSTest : public oatpp::test::UnitTest {
12+
public:
13+
14+
WSTest():UnitTest("TEST[WSTest]"){}
15+
void onRun() override;
16+
17+
};
18+
19+
20+
#endif //MY_PROJECT_WEBSOCKETTEST_HPP

‎client-mbedtls/test/tests.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
#include "WSTest.hpp"
3+
4+
#include "oatpp-test/UnitTest.hpp"
5+
#include <iostream>
6+
7+
8+
void runTests() {
9+
OATPP_RUN_TEST(WSTest);
10+
}
11+
12+
int main() {
13+
14+
oatpp::base::Environment::init();
15+
16+
runTests();
17+
18+
/* Print how much objects were created during app running, and what have left-probably leaked */
19+
/* Disable object counting for release builds using '-D OATPP_DISABLE_ENV_OBJECT_COUNTERS' flag for better performance */
20+
std::cout << "\nEnvironment:\n";
21+
std::cout << "objectsCount = " << oatpp::base::Environment::getObjectsCount() << "\n";
22+
std::cout << "objectsCreated = " << oatpp::base::Environment::getObjectsCreated() << "\n\n";
23+
24+
OATPP_ASSERT(oatpp::base::Environment::getObjectsCount() == 0);
25+
26+
oatpp::base::Environment::destroy();
27+
28+
return 0;
29+
}

0 commit comments

Comments
 (0)
Please sign in to comment.