H5 WebSocket

2017-11-09  本文已影响0人  不语翕
<!DOCTYPE html>
<html>
    <head>
       <script type="text/javascript">
         function WebSocketTest(){
            if("WebSocket" in window){
                alert("WebSocket is supported by your Browser!");
                var ws = new WebSocket("ws://localhost:9998/echo");
                ws.onopen = function(){
                    ws.send("Message to send ");
                    alert("Message is sent...");
                };
                ws.onmessage = function(evt){
                    var received_msg = evt.data;
                    alert("Message is received...");
                };
                ws.onclose = function(){
                    alert("Connection is closed...");
                };
            }
            else{
                alert("WebSocket NOT supported by your Browser!");
            }
         }
       </script>
    </head>
    <body>
        <div id="sse">
            <a href="javascript:WebSocketTest()">Run WebSocket</a>
        </div>
    </body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读