jeudi 23 avril 2015

How to open a html file from websocket server?

I am using a websocket server to parse some information from a program writing with c++ to a html page. I am using the websocket server sample from POCO, my problem is that the html page is generated from the web socket while i want to have the web page dependent from the websocket and that the web client can call the web socket server to give him data whenever it lunches an event like a click of a button.

I tried to call the websocket server from the html page but i think the problem is in the server, how can a server know that a certain page is calling him?

I used this script in my html page to call the websocket whenever i click a button.

function myFunction()
{

     var soc_di="ws://127.0.0.1:9980/ws"

  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);  
  }

}

and here is the code when the websocket server generate the html page

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>";
    }
};

Aucun commentaire:

Enregistrer un commentaire