WebSocket 相关知识

时间:2014-05-14 09:29:12   收藏:0   阅读:401

检测浏览器是否支持WebSocket

bubuko.com,布布扣
var WebSocketsExist = true;
            try {
                var dummy = new WebSocket("ws://localhost:8989/test");
            } catch (ex) {
                try
                {
                    webSocket = new MozWebSocket("ws://localhost:8989/test");
                }
                catch(ex)
                {
                    WebSocketsExist = false;
                }
            }

            if (WebSocketsExist) {
                // 支持
            } else {
                // 不支持
            }
bubuko.com,布布扣

第二种检测WebSocket支持情况的写法:

bubuko.com,布布扣
function IstSupportWebSocket() {
    if (window.WebSocket) {
        //supported
    } else {
        // not supported
    }
}
bubuko.com,布布扣

ws:// --- WebSocket连接

wss:// --- 安全的WebSocket连接

创建对象、连接服务器

bubuko.com,布布扣
url = "ws://localhost:8080/echo";

w = new WebSocket(url);
bubuko.com,布布扣

WebSocket 相关知识,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!