I'm trying to set up a web server using uvw, a wrapper around libuv. I've got the server to a point, where i can send out simple responses, and it works without issues. I wanted to add a SSE route, but no matter what I've tried, i cannot get the browser to properly receive the events I'm trying to send out. Is there something I'm missing about sse? I've already had success adding delay to a normal html response, but no matter what i change, the client (I'm using Javascripts EventSource) doesn't report any events, and neither does the network tab of the developer tools.
My code: main.cpp
#include <memory>
#include <map>
#include <uvw.hpp>
#include <functional>
#include <iostream>
#include <fstream>
using HttpRequestHandler = std::function<void(std::string_view header, uvw::TCPHandle& socket)>;
// Global (yeah) registry of http request handlers
std::map<std::string, HttpRequestHandler> httpHandlers;
// Utility functions for sending messages to tcp sockets
void send (uvw::TCPHandle& target, std::string message) {
target.tryWrite(message.data(), message.size());
}
void send (std::shared_ptr<uvw::TCPHandle> target, std::string message) {
send(*target, message);
}
int main () {
// Create the loop
auto loop = uvw::Loop::getDefault();
// Make a handler for our server
auto tcp = loop->resource<uvw::TCPHandle>();
// Setup logic for incoming connections
tcp->on<uvw::ListenEvent>([](const uvw::ListenEvent& event, uvw::TCPHandle& server){
std::shared_ptr<uvw::TCPHandle> connection = server.loop().resource<uvw::TCPHandle>();
// Setup dealing with the other side disconnecting
connection->once<uvw::EndEvent>([](const auto&, uvw::TCPHandle& connection){ connection.close(); });
// Setup loging errors
connection->on<uvw::ErrorEvent>([](const uvw::ErrorEvent& err, uvw::TCPHandle& connection) {
std::cerr << "Error @ " << connection.sock().ip << ':' << connection.sock().port << "; Message: "
<< err.what() << '\n';
});
// Setup dealing with the request
connection->on<uvw::DataEvent>([](const uvw::DataEvent& event, uvw::TCPHandle& connection){
std::string_view msg {event.data.get(), event.length};
// Extract the request path
const auto firstSpaceIndex = msg.find(' ');
const auto secondSpaceIndex = msg.find(' ', firstSpaceIndex+1);
std::string_view requestPath {msg.data()+firstSpaceIndex+1, secondSpaceIndex-firstSpaceIndex-1};
// TODO: Authenticate the other side
// Use the apropriate handler
if (httpHandlers.contains(std::string(requestPath))) {
httpHandlers.at(std::string(requestPath))(msg, connection);
}
// Or if there's no good handler, just 404
else {
std::string http404Message = "HTTP/1.1 404 Not Found\r\n\r\n";
connection.write(http404Message.data(), http404Message.size());
connection.close();
}
});
// Connect the prepared connection object with an actual request
server.accept(*connection);
// Start reading the data we're getting
connection->read();
});
// Select a port
tcp->bind("127.0.0.1", 4242);
// and connect to it
tcp->listen();
// Open the file with the response of our index page
std::ifstream indexPage ("resources/page.html");
std::string indexPageMessage {std::istreambuf_iterator<char>(indexPage), std::istreambuf_iterator<char>()};
// Register the index handler
httpHandlers["/"] = [message = indexPageMessage](std::string_view header, uvw::TCPHandle& socket) -> void {
socket.write(const_cast<char*>(message.data()), static_cast<unsigned int>(message.size()));
socket.close();
};
// Register the SSE route handler
httpHandlers["/gameEvents"] = [](std::string_view header, uvw::TCPHandle& socket) -> void {
std::cout << "Connecting to gameEvents\n";
// Setup the timer
auto timer = socket.loop().resource<uvw::TimerHandle>();
// Send the headerss
send(socket,
"HTTP/1.1 200 OK\r\n"
"Cache-Control: no-cache\r\n"
"Content-Type: text/event-stream\r\n"
"\r\n\r\n"
);
// Deal with other side closing
socket.on<uvw::CloseEvent>([timer = timer->shared_from_this()](auto& ev, auto& socket) {
timer->close();
socket.close();
});
// React to timer ticks
timer->on<uvw::TimerEvent>([socket = socket.shared_from_this()](auto& event, auto& timer){
if(socket->writable()) {
std::cout << "timer sending timer event\n";
send(socket, "event: timer\n");
} else {
std::cout << "timer encountered unwriteable socket, ignoring for now\n";
}
});
// Trigger the above handler every 5 seconds
timer->start(uvw::TimerHandle::Time{510}, uvw::TimerHandle::Time{5000});
};
// Start the show
loop->run();
// Cleanup after ourselfs
tcp->close();
}
and resources/page.html
HTTP/1.1 200 OK
Content-Type: text/html;charset=utf-8
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Page</title>
</head>
<body>
<h2>Hello, world!</h2>
<script>
const myEventStream = new EventSource("/gameEvents");
myEventStream.addEventListener('message', ev => {
console.log('sse event');
})
</script>
</body>
</html>