![](/img/trans.png)
[英]How to solve spacebrew error: the server responded with a status of 426 (Upgrade Required)?
[英]Failed to load resource: the server responded with a status of 426 (Upgrade Required)
我嘗試使用此鏈接的教程http://web-engineering.info/node/57
但是當我執行 node server.js 並打開瀏覽器http://localhost:3434它說需要升級。 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);
});
});
你必須在瀏覽器中打開你的 index.html 而不是http://127.0.0.1:3434它是一個 websocket 服務器。 您正在嘗試與 websocket 服務器建立 http 連接。
很可能您在 localhost:3434 的服務器套接字不支持 websocket,因此連接被客戶端瀏覽器終止。
此錯誤表明在 localhost:3434 上您正在運行無法“升級”到 websocket 的 HTTP 服務器。
(因為簡單的 http 和 websocket 都是從一個簡單的 http 請求開始的。在那個 http 請求中,客戶端要求服務器切換到 websocket 協議。)
你應該添加這個嗎?
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);
});
參考https://www.npmjs.com/package/websocket.io#passing-in-requests
也檢查這個 SO 什么是 http 升級?
我嘗試攔截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
對於使用 npmjs 文檔的我來說,我去復制粘貼流氓。 然后,調試我的客戶端請求時,我注意到我使用的 URL 參數不是預期的字符串。
但是,shark_s 的回答幫助提醒我去查看控制台並再次解釋錯誤 - 所以謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.