如何从POCO websocket服务器解析html页面?

时间:2015-04-23 14:54:27

标签: c++ html5 websocket poco

我正在使用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地址的事件从页面调用websocket服务器后调用web套接字

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 个答案:

答案 0 :(得分:-2)

您可以在此处找到答案:https://github.com/paulreimer/ofxWebUI/blob/master/src/ofxWebUIRequestHandler.h

基本上你需要做的就是得到这个字符串:

std::string url = request.getURI();