Click here to Skip to main content
15,885,159 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello programmers, I try to use websocket,so I write a html like this:

HTML
<html>
    <body onload="init()">   <input id="su" value="sendit"  type="submit"  />   
    <script> 
    var WsHandler={    };  
    function init()
    {           
      WsHandler.ws= new WebSocket( "wss://192.168.1.176:8443/baseapp/wss" );  
      WsHandler.ws.onopen = function(evt)
            {  
                WsHandler.ws.onmessage =
                                function(e){ console.log('recv: '+ e.data); };
                WsHandler.SendMsg=WsHandler.ws.send;
          };

          var a=0;
         function sendit(){ WsHandler.SendMsg("v"+a++); }
        document.getElementById("su").addEventListener("click",sendit,false);  
    }
    </script>
    </body>
</html>


when I click the button,I get an error:
TypeError: 'send' called on an object that does not implement interface WebSocket.

so I write function sendit() like this:
function sendit() { WsHandler.ws.send("v"+a++ ); }

the websocket can send data now.

why I can not use WsHandler.SendMsg() to send message ?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900