[英]Failed to load resource: the server responded with a status of 426 (Upgrade Required)
I tried to go with the tutorial of this link http://web-engineering.info/node/57我尝试使用此链接的教程http://web-engineering.info/node/57
But when I execute node server.js and open the browser http://localhost:3434 it says upgrade required.但是当我执行 node server.js 并打开浏览器http://localhost:3434它说需要升级。 The server.js file is: server.js 文件是:
var WebSocketServer = require('ws').Server,
wss = new WebSocketServer({port: 3434});
wss.broadcast = function (data) {
var i = 0, n = this.clients ? this.clients.length : 0, client = null;
for (; i < n; i++) {
client = this.clients[i];
if (client.readyState === client.OPEN) {
client.send(data);
}
else console.error('Error: the client state is ' + client.readyState);
}
};
wss.on('connection', function (ws) {
ws.on('message', function (message) {
wss.broadcast(message);
});
});
you have to open your index.html in browser not http://127.0.0.1:3434 its a websocket server.你必须在浏览器中打开你的 index.html 而不是http://127.0.0.1:3434它是一个 websocket 服务器。 You are trying to make a http connection to a websocket server.您正在尝试与 websocket 服务器建立 http 连接。
Most probably your server socket at localhost:3434 don't have support for websocket, so the connection is terminated by the client browser.很可能您在 localhost:3434 的服务器套接字不支持 websocket,因此连接被客户端浏览器终止。
This error indicates that on localhost:3434 you are running a HTTP server which is incapable to "upgrade" to websocket.此错误表明在 localhost:3434 上您正在运行无法“升级”到 websocket 的 HTTP 服务器。
(Since both simple http and websocket begins with a simple http request. In that http request the client ask the server to switch to websocket protocol.) (因为简单的 http 和 websocket 都是从一个简单的 http 请求开始的。在那个 http 请求中,客户端要求服务器切换到 websocket 协议。)
Should you add this ?你应该添加这个吗?
var ws = require('websocket.io')
, server = new ws.Server()
// … somewhere in your http server code
server.on('upgrade', function (req, socket, head) {
server.handleUpgrade(req, socket, head);
});
ref https://www.npmjs.com/package/websocket.io#passing-in-requests参考https://www.npmjs.com/package/websocket.io#passing-in-requests
Check this SO too What is an http upgrade?也检查这个 SO 什么是 http 升级?
I tried intercepting the http request我尝试拦截http请求
var ws = require('websocket.io')
, http = require('http').createServer().listen(3000)
, server = ws.attach(http)
server.on('connection', function (socket) {
socket.on('message', function () { });
socket.on('close', function () { });
});
https://www.npmjs.com/package/websocket.io#passing-in-requests https://www.npmjs.com/package/websocket.io#passing-in-requests
For me using the npmjs documentation, I went copy-paste rogue.对于使用 npmjs 文档的我来说,我去复制粘贴流氓。 Then, debugging my client-side request I noted that the URL parameter I was using was not a string as expected.然后,调试我的客户端请求时,我注意到我使用的 URL 参数不是预期的字符串。
But it was shark_s's answer that helped remind me to go look at the console and interpret the error again- so thanks.但是,shark_s 的回答帮助提醒我去查看控制台并再次解释错误 - 所以谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.