作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试实现一个简单的 HTML5 WebSocket,但是根本没有建立连接,并且在创建 WebSocket 对象时出现以下错误
WebSocket connection to 'ws://localhost:52312/' failed: Error during WebSocket handshake: Unexpected response code: 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)
});
最佳答案
根据https://www.rfc-editor.org/rfc/rfc6455#section-4.1
Once the client's opening handshake has been sent, the client MUSTwait for a response from the server before sending any further data.The client MUST validate the server's response as follows:
- If the status code received from the server is not 101, theclient handles the response per HTTP [RFC2616] procedures. Inparticular, the client might perform authentication if itreceives a 401 status code; the server might redirect the clientusing a 3xx status code (but clients are not required to followthem), etc. Otherwise, proceed as follows.
wss://
协议(protocol))。
localhost:52312
以代码 200 响应 - 如您在上面看到的,websocket 标准在此响应中没有定义任何意义。因此,客户有权提出异常(exception)。问题出在服务器上。
关于html - WebSocket 连接到 'ws://localhost:52312/' 失败 : Error during WebSocket handshake: Unexpected response code: 200,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46639812/
我正在开发一个 voip 调用应用程序。我需要做的是在接到来电时将 Activity 带到前台。我在应用程序中使用 Twilio,并在收到推送消息时开始调用。 问题是我试图在接到任何电话时显示 Act
我是一名优秀的程序员,十分优秀!