diff --git a/examples/broadcast_server/broadcast_server.cpp b/examples/broadcast_server/broadcast_server.cpp index 5a1f6535..884697fa 100644 --- a/examples/broadcast_server/broadcast_server.cpp +++ b/examples/broadcast_server/broadcast_server.cpp @@ -28,14 +28,15 @@ int main(int argc, char** argv) const std::string& str, size_t wireSize, const ix::WebSocketErrorInfo& error, - const ix::WebSocketCloseInfo& closeInfo, - const ix::WebSocketHttpHeaders& headers) + const ix::WebSocketOpenInfo& openInfo, + const ix::WebSocketCloseInfo& closeInfo) { if (messageType == ix::WebSocket_MessageType_Open) { std::cerr << "New connection" << std::endl; + std::cerr << "Uri: " << openInfo.uri << std::endl; std::cerr << "Headers:" << std::endl; - for (auto it : headers) + for (auto it : openInfo.headers) { std::cerr << it.first << ": " << it.second << std::endl; } diff --git a/examples/chat/cmd_websocket_chat.cpp b/examples/chat/cmd_websocket_chat.cpp index 9100d95b..11cb340f 100644 --- a/examples/chat/cmd_websocket_chat.cpp +++ b/examples/chat/cmd_websocket_chat.cpp @@ -87,8 +87,8 @@ namespace const std::string& str, size_t wireSize, const ix::WebSocketErrorInfo& error, - const ix::WebSocketCloseInfo& closeInfo, - const ix::WebSocketHttpHeaders& headers) + const ix::WebSocketOpenInfo& openInfo, + const ix::WebSocketCloseInfo& closeInfo) { std::stringstream ss; if (messageType == ix::WebSocket_MessageType_Open) diff --git a/examples/echo_server/echo_server.cpp b/examples/echo_server/echo_server.cpp index f20428e5..5d9b21e7 100644 --- a/examples/echo_server/echo_server.cpp +++ b/examples/echo_server/echo_server.cpp @@ -28,14 +28,15 @@ int main(int argc, char** argv) const std::string& str, size_t wireSize, const ix::WebSocketErrorInfo& error, - const ix::WebSocketCloseInfo& closeInfo, - const ix::WebSocketHttpHeaders& headers) + const ix::WebSocketOpenInfo& openInfo, + const ix::WebSocketCloseInfo& closeInfo) { if (messageType == ix::WebSocket_MessageType_Open) { std::cerr << "New connection" << std::endl; + std::cerr << "Uri: " << openInfo.uri << std::endl; std::cerr << "Headers:" << std::endl; - for (auto it : headers) + for (auto it : openInfo.headers) { std::cerr << it.first << ": " << it.second << std::endl; } diff --git a/examples/ping_pong/ping_pong.cpp b/examples/ping_pong/ping_pong.cpp index aabdaeaf..04a09fc1 100644 --- a/examples/ping_pong/ping_pong.cpp +++ b/examples/ping_pong/ping_pong.cpp @@ -58,8 +58,8 @@ namespace const std::string& str, size_t wireSize, const ix::WebSocketErrorInfo& error, - const ix::WebSocketCloseInfo& closeInfo, - const ix::WebSocketHttpHeaders& headers) + const ix::WebSocketOpenInfo& openInfo, + const ix::WebSocketCloseInfo& closeInfo) { std::stringstream ss; if (messageType == ix::WebSocket_MessageType_Open) diff --git a/examples/ws_connect/ws_connect.cpp b/examples/ws_connect/ws_connect.cpp index 89a70714..4a80e983 100644 --- a/examples/ws_connect/ws_connect.cpp +++ b/examples/ws_connect/ws_connect.cpp @@ -61,15 +61,16 @@ namespace const std::string& str, size_t wireSize, const ix::WebSocketErrorInfo& error, - const ix::WebSocketCloseInfo& closeInfo, - const ix::WebSocketHttpHeaders& headers) + const ix::WebSocketOpenInfo& openInfo, + const ix::WebSocketCloseInfo& closeInfo) { std::stringstream ss; if (messageType == ix::WebSocket_MessageType_Open) { log("ws_connect: connected"); + std::cout << "Uri: " << openInfo.uri << std::endl; std::cout << "Handshake Headers:" << std::endl; - for (auto it : headers) + for (auto it : openInfo.headers) { std::cout << it.first << ": " << it.second << std::endl; } diff --git a/ixwebsocket/IXWebSocket.cpp b/ixwebsocket/IXWebSocket.cpp index f5849e36..0ded5def 100644 --- a/ixwebsocket/IXWebSocket.cpp +++ b/ixwebsocket/IXWebSocket.cpp @@ -40,9 +40,8 @@ namespace ix [this](uint16_t code, const std::string& reason, size_t wireSize) { _onMessageCallback(WebSocket_MessageType_Close, "", wireSize, - WebSocketErrorInfo(), - WebSocketCloseInfo(code, reason), - WebSocketHttpHeaders()); + WebSocketErrorInfo(), WebSocketOpenInfo(), + WebSocketCloseInfo(code, reason)); } ); } @@ -119,8 +118,9 @@ namespace ix } _onMessageCallback(WebSocket_MessageType_Open, "", 0, - WebSocketErrorInfo(), WebSocketCloseInfo(), - status.headers); + WebSocketErrorInfo(), + WebSocketOpenInfo(status.uri, status.headers), + WebSocketCloseInfo()); return status; } @@ -138,8 +138,9 @@ namespace ix } _onMessageCallback(WebSocket_MessageType_Open, "", 0, - WebSocketErrorInfo(), WebSocketCloseInfo(), - status.headers); + WebSocketErrorInfo(), + WebSocketOpenInfo(status.uri, status.headers), + WebSocketCloseInfo()); return status; } @@ -184,8 +185,8 @@ namespace ix connectErr.reason = status.errorStr; connectErr.http_status = status.http_status; _onMessageCallback(WebSocket_MessageType_Error, "", 0, - connectErr, WebSocketCloseInfo(), - WebSocketHttpHeaders()); + connectErr, WebSocketOpenInfo(), + WebSocketCloseInfo()); std::this_thread::sleep_for(duration); } @@ -240,8 +241,8 @@ namespace ix webSocketErrorInfo.decompressionError = decompressionError; _onMessageCallback(webSocketMessageType, msg, wireSize, - webSocketErrorInfo, WebSocketCloseInfo(), - WebSocketHttpHeaders()); + webSocketErrorInfo, WebSocketOpenInfo(), + WebSocketCloseInfo()); WebSocket::invokeTrafficTrackerCallback(msg.size(), true); }); diff --git a/ixwebsocket/IXWebSocket.h b/ixwebsocket/IXWebSocket.h index 12b616f5..5af2c938 100644 --- a/ixwebsocket/IXWebSocket.h +++ b/ixwebsocket/IXWebSocket.h @@ -41,6 +41,20 @@ namespace ix WebSocket_MessageType_Pong = 5 }; + struct WebSocketOpenInfo + { + std::string uri; + WebSocketHttpHeaders headers; + + WebSocketOpenInfo(const std::string& u = std::string(), + const WebSocketHttpHeaders& h = WebSocketHttpHeaders()) + : uri(u) + , headers(h) + { + ; + } + }; + struct WebSocketCloseInfo { uint16_t code; @@ -59,8 +73,9 @@ namespace ix const std::string&, size_t wireSize, const WebSocketErrorInfo&, - const WebSocketCloseInfo&, - const WebSocketHttpHeaders&)>; + const WebSocketOpenInfo&, + const WebSocketCloseInfo&)>; + using OnTrafficTrackerCallback = std::function; class WebSocket diff --git a/ixwebsocket/IXWebSocketHandshake.cpp b/ixwebsocket/IXWebSocketHandshake.cpp index ad9ebfbf..d38e2400 100644 --- a/ixwebsocket/IXWebSocketHandshake.cpp +++ b/ixwebsocket/IXWebSocketHandshake.cpp @@ -380,7 +380,7 @@ namespace ix } } - return WebSocketInitResult(true, status, "", headers); + return WebSocketInitResult(true, status, "", headers, path); } WebSocketInitResult WebSocketHandshake::serverHandshake(int fd) @@ -491,6 +491,6 @@ namespace ix return WebSocketInitResult(false, 0, std::string("Failed sending response to ") + remote); } - return WebSocketInitResult(true, 200, "", headers); + return WebSocketInitResult(true, 200, "", headers, uri); } } diff --git a/ixwebsocket/IXWebSocketHandshake.h b/ixwebsocket/IXWebSocketHandshake.h index 2d9054c3..01f27572 100644 --- a/ixwebsocket/IXWebSocketHandshake.h +++ b/ixwebsocket/IXWebSocketHandshake.h @@ -26,16 +26,19 @@ namespace ix int http_status; std::string errorStr; WebSocketHttpHeaders headers; + std::string uri; WebSocketInitResult(bool s = false, int status = 0, const std::string& e = std::string(), - WebSocketHttpHeaders h = WebSocketHttpHeaders()) + WebSocketHttpHeaders h = WebSocketHttpHeaders(), + const std::string& u = std::string()) { success = s; http_status = status; errorStr = e; headers = h; + uri = u; } }; diff --git a/ixwebsocket/IXWebSocketServer.h b/ixwebsocket/IXWebSocketServer.h index be3c4f34..ae01dd88 100644 --- a/ixwebsocket/IXWebSocketServer.h +++ b/ixwebsocket/IXWebSocketServer.h @@ -4,11 +4,6 @@ * Copyright (c) 2018 Machine Zone, Inc. All rights reserved. */ -// TODO -// pass to callback PATH -// pass connection success too to callback -// - #pragma once #include // pair diff --git a/makefile b/makefile index 03134b1f..3889703b 100644 --- a/makefile +++ b/makefile @@ -15,6 +15,8 @@ build: (cd examples/chat ; mkdir -p build ; cd build ; cmake .. ; make) (cd examples/ping_pong ; mkdir -p build ; cd build ; cmake .. ; make) (cd examples/ws_connect ; mkdir -p build ; cd build ; cmake .. ; make) + (cd examples/echo_server ; mkdir -p build ; cd build ; cmake .. ; make) + (cd examples/broadcast_server ; mkdir -p build ; cd build ; cmake .. ; make) # That target is used to start a node server, but isn't required as we have # a builtin C++ server started in the unittest now @@ -26,3 +28,4 @@ test: (cd test && sh run.sh) .PHONY: test +.PHONY: build diff --git a/test/IXWebSocketServerTest.cpp b/test/IXWebSocketServerTest.cpp index ab783c85..f2f5ef64 100644 --- a/test/IXWebSocketServerTest.cpp +++ b/test/IXWebSocketServerTest.cpp @@ -27,14 +27,15 @@ namespace ix const std::string& str, size_t wireSize, const ix::WebSocketErrorInfo& error, - const ix::WebSocketCloseInfo& closeInfo, - const ix::WebSocketHttpHeaders& headers) + const ix::WebSocketOpenInfo& openInfo, + const ix::WebSocketCloseInfo& closeInfo) { if (messageType == ix::WebSocket_MessageType_Open) { std::cerr << "New connection" << std::endl; + std::cerr << "Uri: " << openInfo.uri << std::endl; std::cerr << "Headers:" << std::endl; - for (auto it : headers) + for (auto it : openInfo.headers) { std::cerr << it.first << ": " << it.second << std::endl; } diff --git a/test/cmd_websocket_chat.cpp b/test/cmd_websocket_chat.cpp index 900cbf82..7841d81d 100644 --- a/test/cmd_websocket_chat.cpp +++ b/test/cmd_websocket_chat.cpp @@ -94,8 +94,8 @@ namespace const std::string& str, size_t wireSize, const ix::WebSocketErrorInfo& error, - const ix::WebSocketCloseInfo& closeInfo, - const ix::WebSocketHttpHeaders& headers) + const ix::WebSocketOpenInfo& openInfo, + const ix::WebSocketCloseInfo& closeInfo) { std::stringstream ss; if (messageType == ix::WebSocket_MessageType_Open) @@ -183,14 +183,15 @@ namespace const std::string& str, size_t wireSize, const ix::WebSocketErrorInfo& error, - const ix::WebSocketCloseInfo& closeInfo, - const ix::WebSocketHttpHeaders& headers) + const ix::WebSocketOpenInfo& openInfo, + const ix::WebSocketCloseInfo& closeInfo) { if (messageType == ix::WebSocket_MessageType_Open) { std::cerr << "New connection" << std::endl; + std::cerr << "Uri: " << openInfo.uri << std::endl; std::cerr << "Headers:" << std::endl; - for (auto it : headers) + for (auto it : openInfo.headers) { std::cerr << it.first << ": " << it.second << std::endl; }