From b2eb07db145a2aee99a17cd81f2be2be1e152736 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Sun, 30 Dec 2018 22:12:13 -0800 Subject: [PATCH] server code has a callback that takes a websocket --- examples/echo_server/echo_server.cpp | 22 +++++++++++++++++++ ixwebsocket/IXWebSocketServer.cpp | 32 +++++++++++----------------- ixwebsocket/IXWebSocketServer.h | 9 ++++++++ 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/examples/echo_server/echo_server.cpp b/examples/echo_server/echo_server.cpp index ccee5327..305ddbe7 100644 --- a/examples/echo_server/echo_server.cpp +++ b/examples/echo_server/echo_server.cpp @@ -19,6 +19,28 @@ int main(int argc, char** argv) } ix::WebSocketServer server(port); + + server.setOnConnectionCallback( + [](ix::WebSocket& webSocket) + { + webSocket.setOnMessageCallback( + [&webSocket](ix::WebSocketMessageType messageType, + const std::string& str, + size_t wireSize, + const ix::WebSocketErrorInfo& error, + const ix::WebSocketCloseInfo& closeInfo, + const ix::WebSocketHttpHeaders& headers) + { + if (messageType == ix::WebSocket_MessageType_Message) + { + std::cout << str << std::endl; + webSocket.send(str); + } + } + ); + } + ); + auto res = server.listen(); if (!res.first) { diff --git a/ixwebsocket/IXWebSocketServer.cpp b/ixwebsocket/IXWebSocketServer.cpp index 0016902b..d22f650f 100644 --- a/ixwebsocket/IXWebSocketServer.cpp +++ b/ixwebsocket/IXWebSocketServer.cpp @@ -9,7 +9,6 @@ #include "IXWebSocket.h" #include -#include #include #include @@ -30,6 +29,11 @@ namespace ix } + void WebSocketServer::setOnConnectionCallback(const OnConnectionCallback& callback) + { + _onConnectionCallback = callback; + } + std::pair WebSocketServer::listen() { struct sockaddr_in server; /* server address information */ @@ -102,8 +106,7 @@ namespace ix if ((clientFd = accept(_serverFd, (struct sockaddr *)&client, &addressLen)) == -1) { std::cerr << "WebSocketServer::run() error accepting connection: " - << strerror(errno) - << std::endl; + << strerror(errno); continue; } @@ -111,29 +114,18 @@ namespace ix } } + // + // FIXME: make sure we never run into reconnectPerpetuallyIfDisconnected + // void WebSocketServer::handleConnection(int fd) { ix::WebSocket webSocket; webSocket.setSocketFileDescriptor(fd); - - webSocket.setOnMessageCallback( - [&webSocket](ix::WebSocketMessageType messageType, - const std::string& str, - size_t wireSize, - const ix::WebSocketErrorInfo& error, - const ix::WebSocketCloseInfo& closeInfo, - const ix::WebSocketHttpHeaders& headers) - { - if (messageType == ix::WebSocket_MessageType_Message) - { - std::cout << str << std::endl; - webSocket.send(str); - } - } - ); - webSocket.start(); + _onConnectionCallback(webSocket); + + // We can probably do better than this busy loop, with a condition variable. for (;;) { std::chrono::duration wait(10); diff --git a/ixwebsocket/IXWebSocketServer.h b/ixwebsocket/IXWebSocketServer.h index 64943795..d562e443 100644 --- a/ixwebsocket/IXWebSocketServer.h +++ b/ixwebsocket/IXWebSocketServer.h @@ -10,14 +10,21 @@ #include #include #include +#include + +#include "IXWebSocket.h" namespace ix { + using OnConnectionCallback = std::function; + class WebSocketServer { public: WebSocketServer(int port = 8080, int backlog = 5); virtual ~WebSocketServer(); + void setOnConnectionCallback(const OnConnectionCallback& callback); + std::pair listen(); void run(); @@ -27,6 +34,8 @@ namespace ix int _port; int _backlog; + OnConnectionCallback _onConnectionCallback; + // socket for accepting connections int _serverFd;