Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/marian-nmt/Simple-WebSocket-Server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreidheim <eidheim@gmail.com>2017-06-28 11:57:29 +0300
committereidheim <eidheim@gmail.com>2017-06-28 11:57:29 +0300
commit1418c0e7b818c63b68837944ab5a624f56072ee3 (patch)
treedfd04070f900d368e46a3e608a0e6a47f97c4843 /wss_examples.cpp
parent2f42ae53b083983f99c7321636effd32e13f6f9b (diff)
Added .clang-format file and applied style to source files
Diffstat (limited to 'wss_examples.cpp')
-rw-r--r--wss_examples.cpp306
1 files changed, 153 insertions, 153 deletions
diff --git a/wss_examples.cpp b/wss_examples.cpp
index 941b0a0..d04f684 100644
--- a/wss_examples.cpp
+++ b/wss_examples.cpp
@@ -1,5 +1,5 @@
-#include "server_wss.hpp"
#include "client_wss.hpp"
+#include "server_wss.hpp"
using namespace std;
@@ -7,157 +7,157 @@ typedef SimpleWeb::SocketServer<SimpleWeb::WSS> WssServer;
typedef SimpleWeb::SocketClient<SimpleWeb::WSS> WssClient;
int main() {
- //WebSocket Secure (WSS)-server at port 8080 using 1 thread
- WssServer server("server.crt", "server.key");
- server.config.port=8080;
-
- //Example 1: echo WebSocket Secure endpoint
- // Added debug messages for example use of the callbacks
- // Test with the following JavaScript:
- // var wss=new WebSocket("wss://localhost:8080/echo");
- // wss.onmessage=function(evt){console.log(evt.data);};
- // wss.send("test");
- auto& echo=server.endpoint["^/echo/?$"];
-
- echo.on_message=[&server](shared_ptr<WssServer::Connection> connection, shared_ptr<WssServer::Message> message) {
- //WssServer::Message::string() is a convenience function for:
- //stringstream data_ss;
- //data_ss << message->rdbuf();
- //auto message_str = data_ss.str();
- auto message_str=message->string();
-
- cout << "Server: Message received: \"" << message_str << "\" from " << (size_t)connection.get() << endl;
-
- cout << "Server: Sending message \"" << message_str << "\" to " << (size_t)connection.get() << endl;
-
- auto send_stream=make_shared<WssServer::SendStream>();
- *send_stream << message_str;
- //server.send is an asynchronous function
- server.send(connection, send_stream, [](const SimpleWeb::error_code& ec){
- if(ec) {
- cout << "Server: Error sending message. " <<
- //See http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference.html, Error Codes for error code meanings
- "Error: " << ec << ", error message: " << ec.message() << endl;
- }
- });
- };
-
- echo.on_open=[](shared_ptr<WssServer::Connection> connection) {
- cout << "Server: Opened connection " << (size_t)connection.get() << endl;
- };
-
- //See RFC 6455 7.4.1. for status codes
- echo.on_close=[](shared_ptr<WssServer::Connection> connection, int status, const string& /*reason*/) {
- cout << "Server: Closed connection " << (size_t)connection.get() << " with status code " << status << endl;
- };
-
- //See http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference.html, Error Codes for error code meanings
- echo.on_error=[](shared_ptr<WssServer::Connection> connection, const SimpleWeb::error_code& ec) {
- cout << "Server: Error in connection " << (size_t)connection.get() << ". " <<
- "Error: " << ec << ", error message: " << ec.message() << endl;
- };
-
- //Example 2: Echo thrice
- // Demonstrating queuing of messages by sending a received message three times back to the client.
- // send_stream2 is automatically queued by the library after the first send call,
- // and send_stream3 is queued after the first send call through its callback
- // Test with the following JavaScript:
- // var wss=new WebSocket("wss://localhost:8080/echo_thrice");
- // wss.onmessage=function(evt){console.log(evt.data);};
- // wss.send("test");
- auto& echo_thrice=server.endpoint["^/echo_thrice/?$"];
- echo_thrice.on_message=[&server](shared_ptr<WssServer::Connection> connection, shared_ptr<WssServer::Message> message) {
- auto message_str=message->string();
-
- auto send_stream1=make_shared<WssServer::SendStream>();
- *send_stream1 << message_str;
- //server.send is an asynchronous function
- server.send(connection, send_stream1, [&server, connection, message_str](const SimpleWeb::error_code& ec) {
- if(!ec) {
- auto send_stream3=make_shared<WssServer::SendStream>();
- *send_stream3 << message_str;
- server.send(connection, send_stream3); //Sent after send_stream1 is sent, and most likely after send_stream2
- }
- });
- //Do not reuse send_stream1 here as it most likely is not sent yet
- auto send_stream2=make_shared<WssServer::SendStream>();
- *send_stream2 << message_str;
- server.send(connection, send_stream2); //Most likely queued, and sent after send_stream1
- };
-
- //Example 3: Echo to all WebSocket Secure endpoints
- // Sending received messages to all connected clients
- // Test with the following JavaScript on more than one browser windows:
- // var wss=new WebSocket("wss://localhost:8080/echo_all");
- // wss.onmessage=function(evt){console.log(evt.data);};
- // wss.send("test");
- auto& echo_all=server.endpoint["^/echo_all/?$"];
- echo_all.on_message=[&server](shared_ptr<WssServer::Connection> /*connection*/, shared_ptr<WssServer::Message> message) {
- auto message_str=message->string();
-
- //echo_all.get_connections() can also be used to solely receive connections on this endpoint
- for(auto &a_connection: server.get_connections()) {
- auto send_stream=make_shared<WssServer::SendStream>();
- *send_stream << message_str;
-
- //server.send is an asynchronous function
- server.send(a_connection, send_stream);
- }
- };
-
- thread server_thread([&server](){
- //Start WSS-server
- server.start();
+ //WebSocket Secure (WSS)-server at port 8080 using 1 thread
+ WssServer server("server.crt", "server.key");
+ server.config.port = 8080;
+
+ //Example 1: echo WebSocket Secure endpoint
+ // Added debug messages for example use of the callbacks
+ // Test with the following JavaScript:
+ // var wss=new WebSocket("wss://localhost:8080/echo");
+ // wss.onmessage=function(evt){console.log(evt.data);};
+ // wss.send("test");
+ auto &echo = server.endpoint["^/echo/?$"];
+
+ echo.on_message = [&server](shared_ptr<WssServer::Connection> connection, shared_ptr<WssServer::Message> message) {
+ //WssServer::Message::string() is a convenience function for:
+ //stringstream data_ss;
+ //data_ss << message->rdbuf();
+ //auto message_str = data_ss.str();
+ auto message_str = message->string();
+
+ cout << "Server: Message received: \"" << message_str << "\" from " << (size_t)connection.get() << endl;
+
+ cout << "Server: Sending message \"" << message_str << "\" to " << (size_t)connection.get() << endl;
+
+ auto send_stream = make_shared<WssServer::SendStream>();
+ *send_stream << message_str;
+ //server.send is an asynchronous function
+ server.send(connection, send_stream, [](const SimpleWeb::error_code &ec) {
+ if(ec) {
+ cout << "Server: Error sending message. " <<
+ //See http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference.html, Error Codes for error code meanings
+ "Error: " << ec << ", error message: " << ec.message() << endl;
+ }
});
-
- //Wait for server to start so that the client can connect
- this_thread::sleep_for(chrono::seconds(1));
-
- //Example 4: Client communication with server
- //Second Client() parameter set to false: no certificate verification
- //Possible output:
- //Server: Opened connection 140184920260656
- //Client: Opened connection
- //Client: Sending message: "Hello"
- //Server: Message received: "Hello" from 140184920260656
- //Server: Sending message "Hello" to 140184920260656
- //Client: Message received: "Hello"
- //Client: Sending close connection
- //Server: Closed connection 140184920260656 with status code 1000
- //Client: Closed connection with status code 1000
- WssClient client("localhost:8080/echo", false);
- client.on_message=[&client](shared_ptr<WssClient::Message> message) {
- auto message_str=message->string();
-
- cout << "Client: Message received: \"" << message_str << "\"" << endl;
-
- cout << "Client: Sending close connection" << endl;
- client.send_close(1000);
- };
-
- client.on_open=[&client]() {
- cout << "Client: Opened connection" << endl;
-
- string message="Hello";
- cout << "Client: Sending message: \"" << message << "\"" << endl;
-
- auto send_stream=make_shared<WssClient::SendStream>();
- *send_stream << message;
- client.send(send_stream);
- };
-
- client.on_close=[](int status, const string& /*reason*/) {
- cout << "Client: Closed connection with status code " << status << endl;
- };
-
- //See http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference.html, Error Codes for error code meanings
- client.on_error=[](const SimpleWeb::error_code& ec) {
- cout << "Client: Error: " << ec << ", error message: " << ec.message() << endl;
- };
-
- client.start();
-
- server_thread.join();
-
- return 0;
+ };
+
+ echo.on_open = [](shared_ptr<WssServer::Connection> connection) {
+ cout << "Server: Opened connection " << (size_t)connection.get() << endl;
+ };
+
+ //See RFC 6455 7.4.1. for status codes
+ echo.on_close = [](shared_ptr<WssServer::Connection> connection, int status, const string & /*reason*/) {
+ cout << "Server: Closed connection " << (size_t)connection.get() << " with status code " << status << endl;
+ };
+
+ //See http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference.html, Error Codes for error code meanings
+ echo.on_error = [](shared_ptr<WssServer::Connection> connection, const SimpleWeb::error_code &ec) {
+ cout << "Server: Error in connection " << (size_t)connection.get() << ". "
+ << "Error: " << ec << ", error message: " << ec.message() << endl;
+ };
+
+ //Example 2: Echo thrice
+ // Demonstrating queuing of messages by sending a received message three times back to the client.
+ // send_stream2 is automatically queued by the library after the first send call,
+ // and send_stream3 is queued after the first send call through its callback
+ // Test with the following JavaScript:
+ // var wss=new WebSocket("wss://localhost:8080/echo_thrice");
+ // wss.onmessage=function(evt){console.log(evt.data);};
+ // wss.send("test");
+ auto &echo_thrice = server.endpoint["^/echo_thrice/?$"];
+ echo_thrice.on_message = [&server](shared_ptr<WssServer::Connection> connection, shared_ptr<WssServer::Message> message) {
+ auto message_str = message->string();
+
+ auto send_stream1 = make_shared<WssServer::SendStream>();
+ *send_stream1 << message_str;
+ //server.send is an asynchronous function
+ server.send(connection, send_stream1, [&server, connection, message_str](const SimpleWeb::error_code &ec) {
+ if(!ec) {
+ auto send_stream3 = make_shared<WssServer::SendStream>();
+ *send_stream3 << message_str;
+ server.send(connection, send_stream3); //Sent after send_stream1 is sent, and most likely after send_stream2
+ }
+ });
+ //Do not reuse send_stream1 here as it most likely is not sent yet
+ auto send_stream2 = make_shared<WssServer::SendStream>();
+ *send_stream2 << message_str;
+ server.send(connection, send_stream2); //Most likely queued, and sent after send_stream1
+ };
+
+ //Example 3: Echo to all WebSocket Secure endpoints
+ // Sending received messages to all connected clients
+ // Test with the following JavaScript on more than one browser windows:
+ // var wss=new WebSocket("wss://localhost:8080/echo_all");
+ // wss.onmessage=function(evt){console.log(evt.data);};
+ // wss.send("test");
+ auto &echo_all = server.endpoint["^/echo_all/?$"];
+ echo_all.on_message = [&server](shared_ptr<WssServer::Connection> /*connection*/, shared_ptr<WssServer::Message> message) {
+ auto message_str = message->string();
+
+ //echo_all.get_connections() can also be used to solely receive connections on this endpoint
+ for(auto &a_connection : server.get_connections()) {
+ auto send_stream = make_shared<WssServer::SendStream>();
+ *send_stream << message_str;
+
+ //server.send is an asynchronous function
+ server.send(a_connection, send_stream);
+ }
+ };
+
+ thread server_thread([&server]() {
+ //Start WSS-server
+ server.start();
+ });
+
+ //Wait for server to start so that the client can connect
+ this_thread::sleep_for(chrono::seconds(1));
+
+ //Example 4: Client communication with server
+ //Second Client() parameter set to false: no certificate verification
+ //Possible output:
+ //Server: Opened connection 140184920260656
+ //Client: Opened connection
+ //Client: Sending message: "Hello"
+ //Server: Message received: "Hello" from 140184920260656
+ //Server: Sending message "Hello" to 140184920260656
+ //Client: Message received: "Hello"
+ //Client: Sending close connection
+ //Server: Closed connection 140184920260656 with status code 1000
+ //Client: Closed connection with status code 1000
+ WssClient client("localhost:8080/echo", false);
+ client.on_message = [&client](shared_ptr<WssClient::Message> message) {
+ auto message_str = message->string();
+
+ cout << "Client: Message received: \"" << message_str << "\"" << endl;
+
+ cout << "Client: Sending close connection" << endl;
+ client.send_close(1000);
+ };
+
+ client.on_open = [&client]() {
+ cout << "Client: Opened connection" << endl;
+
+ string message = "Hello";
+ cout << "Client: Sending message: \"" << message << "\"" << endl;
+
+ auto send_stream = make_shared<WssClient::SendStream>();
+ *send_stream << message;
+ client.send(send_stream);
+ };
+
+ client.on_close = [](int status, const string & /*reason*/) {
+ cout << "Client: Closed connection with status code " << status << endl;
+ };
+
+ //See http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference.html, Error Codes for error code meanings
+ client.on_error = [](const SimpleWeb::error_code &ec) {
+ cout << "Client: Error: " << ec << ", error message: " << ec.message() << endl;
+ };
+
+ client.start();
+
+ server_thread.join();
+
+ return 0;
}