Added examples for a simple WebSocket client and Server.

Removed automatic ping pong response, and disconnection from fr::WebSocket, as it should be handled by the library user, really.

Updated ReadMe.
This commit is contained in:
Unknown
2018-03-03 16:02:17 +00:00
parent fa843b57c8
commit 13f709aebb
8 changed files with 24 additions and 52 deletions
@@ -0,0 +1,8 @@
add_executable(simple_http_client SimpleHttpClient.cpp)
target_link_libraries(simple_http_client frnetlib)
add_executable(simple_http_server SimpleHttpServer.cpp)
target_link_libraries(simple_http_server frnetlib)
install(TARGETS simple_http_client simple_http_server
DESTINATION "bin")
@@ -0,0 +1,54 @@
//
// Created by fred.nicolson on 01/02/18.
//
#include <iostream>
#include <frnetlib/TcpSocket.h>
#include <frnetlib/HttpRequest.h>
#include <frnetlib/URL.h>
#include <frnetlib/HttpResponse.h>
int main()
{
//Get an address to query from stdin
std::string url;
std::cout << "Enter URL: ";
std::cin >> url;
//Parse it into something easy to use
fr::URL parsed_url(url);
if(parsed_url.get_port().empty())
{
std::cerr << "No schema or port specified. Unable to connect." << std::endl;
return EXIT_FAILURE;
}
//Try to connect to the parsed address
fr::TcpSocket socket;
if(socket.connect(parsed_url.get_host(), parsed_url.get_port(), {}) != fr::Socket::Success)
{
std::cerr << "Failed to connect to the specified URL" << std::endl;
return EXIT_FAILURE;
}
//Construct a request, requesting the user provided URI
fr::HttpRequest request;
request.set_uri(parsed_url.get_uri());
if(socket.send(request) != fr::Socket::Success)
{
std::cerr << "Failed to send HTTP request" << std::endl;
return EXIT_FAILURE;
}
//Now wait for a response
fr::HttpResponse response;
if(socket.receive(response) != fr::Socket::Success)
{
std::cerr << "Failed to receive HTTP response" << std::endl;
return EXIT_FAILURE;
}
//Print out the response body
std::cout << response.get_body() << std::endl;
}
@@ -0,0 +1,52 @@
//
// Created by fred.nicolson on 01/02/18.
//
#include <frnetlib/HttpRequest.h>
#include <frnetlib/HttpResponse.h>
#include <frnetlib/TcpListener.h>
#include <iostream>
int main()
{
fr::Socket::Status err;
fr::TcpSocket client;
fr::TcpListener listener;
//Bind to a port
if((err = listener.listen("8081")) != fr::Socket::Success)
{
std::cerr << "Failed to bind to port: " << err << std::endl;
return EXIT_FAILURE;
}
while(true)
{
//Accept a new connection
if((err = listener.accept(client)) != fr::Socket::Success)
{
std::cerr << "Failed to accept new connection: " << err << std::endl;
continue;
}
//Receive client HTTP request
fr::HttpRequest request;
if((err = client.receive(request)) != fr::Socket::Success)
{
std::cerr << "Failed to receive request from client: " << err << std::endl;
}
//Construct a response
fr::HttpResponse response;
response.set_body("<h1>Hello, World!</h1>");
//Send it
if((err = client.send(response)) != fr::Socket::Success)
{
std::cerr << "Failed to send response to client: " << err << std::endl;
}
//Close connection
client.disconnect();
}
}