我試圖實現一個簡單的HTML5 WebSocket,但連接根本沒有建立&我越來越下面誤差的WebSocket對象被創建WebSocket連接到'ws:// localhost:52312 /'失敗:WebSocket握手期間出錯:意外的響應代碼:200
WebSocket連接到 'WS://本地主機:52312 /' 失敗:WebSocket的握手期間錯誤:意外響應代碼:200
下面是代碼
document.addEventListener("DOMContentLoaded", function (event) {
function runHub() {
if ("WebSocket" in window) {
console.log('WebSocket is supported by your browser.');
//var serviceUrl = 'ws://localhost:52312/';
var serviceUrl = 'ws://localhost:52312/home/GetNotificationCount';
var protocol = 'Chat-1.0';
var socket = new WebSocket(serviceUrl);
socket.onopen = function() {
console.log('Connection Established!');
};
socket.onclose = function (error) {
console.log('Connection Closed!');
console.log('Error Occured: ' + JSON.stringify(error));
};
socket.onerror = function (error) {
console.log('Error Occured: ' + JSON.stringify(error));
};
socket.onmessage = function (e) {
if (typeof e.data === "string") {
console.log('String message received: ' + e.data);
}
else if (e.data instanceof ArrayBuffer) {
console.log('ArrayBuffer received: ' + e.data);
}
else if (e.data instanceof Blob) {
console.log('Blob received: ' + e.data);
}
};
if (!socket.readyState === WebSocket.CLOSED) {
socket.send($('#notificationCount').text);
//socket.close();
}
}
}
var run = setInterval(function() {
runHub();
}, 10000)
});
有沒有人有這方面的想法?
你連接了什麼?看起來像你沒有連接到想要websocket連接的東西,就像一個普通的web服務器。 – xaxxon