如何从 POCO websocket 服务器解析 html 页面?
Posted
技术标签:
【中文标题】如何从 POCO websocket 服务器解析 html 页面?【英文标题】:How to parse html page from POCO websocket server? 【发布时间】:2015-04-23 14:54:26 【问题描述】:我正在使用 POCO 编写一个 websocket 服务器,按照 POCO 文档中的示例,我可以像这样从服务器生成 html 页面
class PageRequestHandler: public HTTPRequestHandler
/// Return a HTML document with some javascript creating
/// a WebSocket connection.
public:
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
response.setChunkedTransferEncoding(true);
response.setContentType("text/html");
std::ostream& ostr = response.send();
ostr << "<html>";
ostr << "<head>";
ostr << "<title>WebSocketServer</title>";
ostr << "<script type=\"text/javascript\">";
ostr << "function WebSocketTest()";
ostr << "";
ostr << " if (\"WebSocket\" in window)";
ostr << " ";
ostr << " var ws = new WebSocket(\"ws://" << request.serverAddress().toString() << "/ws\");";
ostr << " ws.onopen = function()";
ostr << " ";
ostr << " ws.send(\"Hello, world!\");";
ostr << " ;";
ostr << " ws.onmessage = function(evt)";
ostr << " ";
ostr << " var msg = evt.data;";
ostr << " alert(\"Message received: \" + msg);";
ostr << " ws.close();";
ostr << " ;";
ostr << " ws.onclose = function()";
ostr << " ";
ostr << " alert(\"WebSocket closed.\");";
ostr << " ;";
ostr << " ";
ostr << " else";
ostr << " ";
ostr << " alert(\"This browser does not support WebSockets.\");";
ostr << " ";
ostr << "";
ostr << "</script>";
ostr << "</head>";
ostr << "<body>";
ostr << " <h1>WebSocket Server</h1>";
ostr << " <p><a href=\"javascript:WebSocketTest()\">Run WebSocket Script</a></p>";
ostr << "</body>";
ostr << "</html>";
;
当我可以在我的导航器中像这样http://127.0.0.1:9980/
的服务器时,我会得到生成的页面。
现在我的问题是我想像这样http://127.0.0.1:9980/index.html
调用我的导航器中的特定页面。
我尝试通过添加一个像这样调用 websocket 地址的事件来调用 web socket,在午餐 websocket 服务器之后,从页面中调用 websocket
function myFunction()
var soc_di="ws://127.0.0.1:9980/ws"
//var soc_di;
// soc_di = new WebSocket(get_appropriate_ws_url(),
// "send-protocol");
try
soc_di.onopen = function()
//document.getElementById("wsdi_statustd").style.backgroundColor = "#40ff40";
document.getElementById("demo").textContent = " websocket connection opened ";
soc_di.onmessage =function got_packet(msg)
//document.getElementById("number").textContent = msg.data + "\n";
document.getElementById("des").textContent = msg.data + "\n";
soc_di.onclose = function()
//document.getElementById("wsdi_statustd").style.backgroundColor = "#ff4040";
document.getElementById("demo").textContent = " websocket connection CLOSED ";
catch(exception)
alert('<p>Error' + exception);
谁能告诉我我做错了什么? 提前致谢。
【问题讨论】:
【参考方案1】:你可以在这里找到答案:https://github.com/paulreimer/ofxWebUI/blob/master/src/ofxWebUIRequestHandler.h
基本上你需要做的是得到这个字符串:
std::string url = request.getURI();
【讨论】:
以上是关于如何从 POCO websocket 服务器解析 html 页面?的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Python 中的 websocket 消息增量解析 JSON?
Nestjs websocket网关,如何从握手中解析签名cookie以进行保护授权?