Wrap up messageHandler, send(message)

This commit is contained in:
Alex Hultman
2018-10-30 03:01:19 +01:00
parent 292476366c
commit f2e2b7107a
5 changed files with 45 additions and 9 deletions
+2
View File
@@ -11,6 +11,8 @@ int main(int argc, char **argv) {
res->end("Hello HTTP!");
}).ws("/*", [](auto *ws, auto *req) {
std::cout << "WebSocket conntected to URL: " << req->getUrl() << std::endl;
}, [](auto *ws, std::string_view message) {
ws->send(message);
}).listen(3000, [](auto *token) {
if (token) {
std::cout << "Listening on port " << 3000 << std::endl;
+4 -2
View File
@@ -52,10 +52,12 @@ public:
}
// this method creates a new websocket context and attaches it to a path
TemplatedApp &ws(std::string pattern, std::function<void(void *, HttpRequest *)> connectHandler) {
TemplatedApp &ws(std::string pattern, std::function<void(void *, HttpRequest *)> connectHandler, std::function<void(uWS::WebSocket<SSL, true> *, std::string_view)> messageHandler) {
// init the websocket context here!
uWS::WebSocketContext<SSL> *webSocketContext = uWS::WebSocketContext<SSL>::create(uWS::Loop::defaultLoop(), (typename StaticDispatch<SSL>::SOCKET_CONTEXT_TYPE *) httpContext);
webSocketContext->getExt()->messageHandler = messageHandler;
return get(pattern, [webSocketContext, this, connectHandler](auto *res, auto *req) {
std::string_view secWebSocketKey = req->getHeader("sec-websocket-key");
@@ -77,7 +79,7 @@ public:
// rely on http context data
// todo: sizeof websocket
WebSocket<SSL> *webSocket = (WebSocket<SSL> *) StaticDispatch<SSL>::static_dispatch(us_ssl_socket_context_adopt_socket, us_socket_context_adopt_socket)(
WebSocket<SSL, true> *webSocket = (WebSocket<SSL, true> *) StaticDispatch<SSL>::static_dispatch(us_ssl_socket_context_adopt_socket, us_socket_context_adopt_socket)(
(typename StaticDispatch<SSL>::SOCKET_CONTEXT_TYPE *) webSocketContext, (typename StaticDispatch<SSL>::SOCKET_TYPE *) res, 150);
webSocket->init();
+22 -1
View File
@@ -3,13 +3,34 @@
#include "WebSocketData.h"
#include "WebSocketProtocol.h"
#include <string_view>
namespace uWS {
template <bool SSL>
template <bool SSL, bool isServer>
struct WebSocket {
private:
public:
void send(std::string_view message) {
// this path should use AsyncSocket with cork and everything
// format the response
char buf[100];
int writeLength = WebSocketProtocol<isServer, WebSocket<SSL, isServer>>::formatMessage(buf, message.data(), message.length(), uWS::OpCode::TEXT, message.length(), false);
us_socket_write((us_socket *) this, buf, writeLength, false);
}
// absolutely not public!
void init() {
// construct us
+11 -6
View File
@@ -13,6 +13,7 @@ namespace uWS {
template <bool SSL>
struct WebSocketContext : StaticDispatch<SSL> {
template <bool> friend struct TemplatedApp;
private:
using SOCKET_CONTEXT_TYPE = typename StaticDispatch<SSL>::SOCKET_CONTEXT_TYPE;
using SOCKET_TYPE = typename StaticDispatch<SSL>::SOCKET_TYPE;
@@ -23,6 +24,10 @@ private:
return (SOCKET_CONTEXT_TYPE *) this;
}
WebSocketContextData<SSL> *getExt() {
return (WebSocketContextData<SSL> *) us_socket_context_ext((SOCKET_CONTEXT_TYPE *) this);
}
// I don't even.. merge this with the context itself!
template <bool isServer>
struct WebSocketProtcolImplementation {
@@ -37,16 +42,16 @@ private:
static bool handleFragment(char *data, size_t length, unsigned int remainingBytes, int opCode, bool fin, uWS::WebSocketState<isServer> *webSocketState, void *s) {
// this path should use AsyncSocket with cork and everything
// this is maybe not the most elegant but who cares
WebSocketContextData<SSL> *webSocketContextData = (WebSocketContextData<SSL> *) us_socket_context_ext(us_socket_get_context((us_socket *) s));
// format the response
char buf[100];
int writeLength = WebSocketProtocol<isServer, WebSocketProtcolImplementation<isServer>>::formatMessage(buf, data, length, (uWS::OpCode) opCode, length, false);
us_socket_write((SOCKET_TYPE *) s, buf, writeLength, false);
// emit message
webSocketContextData->messageHandler((WebSocket<SSL, true> *) s, std::string_view(data, length));
// the only thing here to check is probably closed
// why does it not do anything immediately on true?
return false;
}
static bool refusePayloadLength(uint64_t length, uWS::WebSocketState<isServer> *wState) {
+6
View File
@@ -1,11 +1,17 @@
#ifndef WEBSOCKETCONTEXTDATA_H
#define WEBSOCKETCONTEXTDATA_H
#include <functional>
#include <string_view>
namespace uWS {
template <bool, bool> struct WebSocket;
template <bool SSL>
struct WebSocketContextData {
std::function<void(WebSocket<SSL, true> *, std::string_view)> messageHandler;
};
}