html5创建websocket对象的通用方法
var ws = null;
var url = null;
var urlPath = "/msg";
var urlJs = "/sockjs/msg";
var transports = [];
if (window.location.protocol == 'http:') {
url = 'ws://' + window.location.host + urlPath;
} else {
url = 'wss://' + window.location.host + urlPath;
}
if ('WebSocket' in window) {
ws = new WebSocket(url);
} else if ('MozWebSocket' in window) {
ws = new MozWebSocket(url);
} else {
ws = new SockJS(urlJs, undefined, {protocols_whitelist : transports});
}
ws.onopen = function() {
log('Socket 连接打开,请稍候...');
};
ws.onmessage = function(event) {
};
ws.onclose = function(event) {
log('Socket 连接已关闭');
};
如果您觉得本文的内容对您的学习有所帮助:
关键字:
html5 websocket