Reputation: 507
I've been struggling to get a basic application going where I use boost to attempt to connect to a local webserver hosted on nodejs using express and socket.io. the idea is to host a socket server and listen for any messages on localhost, port 3000. when the c++ program opens, it is supposed to send a message using these command line parameters:
websocket-client-sync 127.0.0.1 3000 "connection"
I've added messages to the console at each step and it seems that it fails at the handshake step. the error I get is this:
Error: end of stream [beast.http:1 at C:\libraries\boost\beast\http\impl\read.hpp:366:13 in function 'read_some']
here is the cpp code, followed by the nodejs code:
Main.cpp
#include <boost/beast.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/beast/http.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <cstdlib>
#include <iostream>
#include <string>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
// Sends a WebSocket message and prints the response
int main(int argc, char** argv)
{
std::cout << "beginning\n";
try
{
std::cout << "Argument Count: " << argc << " \n";
std::cout << "Path: " << argv[0] << "\n";
std::cout << "Command: " << argv[1] << "\n";
std::cout << "Host: " << argv[2] << "\n";
std::cout << "Port: " << argv[3] << "\n";
std::cout << "Message: " << argv[4] << "\n";
// Check command line arguments.
if (argc != 5)
{
// This displays if there is an error
std::cerr <<
"Usage: websocket-client-sync <host> <port> <text>\n" <<
"Example:\n" <<
" websocket-client-sync echo.websocket.org 80 \"Hello, world!\"\n";
return EXIT_FAILURE;
}
std::string host = argv[2];
auto const port = argv[3];
auto const text = argv[4];
// The io_context is required for all I/O
net::io_context ioc;
std::cout << "IO Context Obtained\n";
// These objects perform our I/O
tcp::resolver resolver{ ioc };
websocket::stream<tcp::socket> ws{ ioc };
std::cout << "IO Performed\n";
// Look up the domain name
auto const results = resolver.resolve(host, port);
std::cout << "Domain name looked up\n";
// Make the connection on the IP address we get from a lookup
auto ep = net::connect(ws.next_layer(), results);
std::cout << "Connection made from IP address\n";
// Update the host_ string. This will provide the value of the
// Host HTTP header during the WebSocket handshake.
// See https://tools.ietf.org/html/rfc7230#section-5.4
host += ':' + std::to_string(ep.port());
std::cout << "Host string updated\n";
// Set a decorator to change the User-Agent of the handshake
ws.set_option(websocket::stream_base::decorator(
[](websocket::request_type& req)
{
req.set(http::field::user_agent,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-client-coro");
}));
std::cout << "User-Agent Changed\n";
// Perform the websocket handshake
ws.handshake(host, "/");
std::cout << "Handshake Performed\n";
// Send the message
ws.write(net::buffer(std::string(text)));
std::cout << "Message Sent\n";
// This buffer will hold the incoming message
beast::flat_buffer buffer;
std::cout << "Incoming Message held by buffer\n";
// Read a message into our buffer
ws.read(buffer);
std::cout << "Message read into buffer \n";
// Close the WebSocket connection
ws.close(websocket::close_code::normal);
std::cout << "Connection Closed Gracefully\n";
// If we get here then the connection is closed gracefully
// The make_printable() function helps print a ConstBufferSequence
std::cout << beast::make_printable(buffer.data()) << std::endl;
std::cout << "ConstBufferSequence Printed\n";
}
catch (std::exception const& e)
{
std::cerr << "Error: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
index.js
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
console.log('a user connected');
socket.on('disconnect', () => {
console.log('user disconnected');
});
socket.on('chat message', (msg) => {
io.emit('chat message', msg);
console.log('chat message');
});
});
server.listen(3000, () => {
console.log('listening on *:3000');
});
Upvotes: 0
Views: 491