server code has a callback that takes a websocket
This commit is contained in:
		| @@ -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) | ||||
|     { | ||||
|   | ||||
| @@ -9,7 +9,6 @@ | ||||
| #include "IXWebSocket.h" | ||||
|  | ||||
| #include <sstream> | ||||
| #include <iostream> | ||||
|  | ||||
| #include <netdb.h> | ||||
| #include <stdio.h> | ||||
| @@ -30,6 +29,11 @@ namespace ix | ||||
|  | ||||
|     } | ||||
|  | ||||
|     void WebSocketServer::setOnConnectionCallback(const OnConnectionCallback& callback) | ||||
|     { | ||||
|         _onConnectionCallback = callback; | ||||
|     } | ||||
|  | ||||
|     std::pair<bool, std::string> 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<double, std::milli> wait(10); | ||||
|   | ||||
| @@ -10,14 +10,21 @@ | ||||
| #include <string> | ||||
| #include <vector> | ||||
| #include <thread> | ||||
| #include <functional> | ||||
|  | ||||
| #include "IXWebSocket.h" | ||||
|  | ||||
| namespace ix  | ||||
| { | ||||
|     using OnConnectionCallback = std::function<void(WebSocket&)>; | ||||
|  | ||||
|     class WebSocketServer { | ||||
|     public: | ||||
|         WebSocketServer(int port = 8080, int backlog = 5); | ||||
|         virtual ~WebSocketServer(); | ||||
|  | ||||
|         void setOnConnectionCallback(const OnConnectionCallback& callback); | ||||
|  | ||||
|         std::pair<bool, std::string> listen(); | ||||
|         void run(); | ||||
|  | ||||
| @@ -27,6 +34,8 @@ namespace ix | ||||
|         int _port; | ||||
|         int _backlog; | ||||
|  | ||||
|         OnConnectionCallback _onConnectionCallback; | ||||
|  | ||||
|         // socket for accepting connections | ||||
|         int _serverFd; | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user