2016-05-12 465 views
0

我試圖去用這個鏈接http://web-engineering.info/node/57無法加載資源:服務器的狀態爲迴應426(需要升級)

的教程但是,當我執行節點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); 
}); 
}); 

回答

0

最有可能在本地主機服務器套接字:3434不具備WebSocket的支持,所以連接是由客戶端瀏覽器終止。

此錯誤表明在localhost:3434上運行的HTTP服務器無法「升級」到websocket。

(由於這兩個簡單的HTTP和WebSocket的始於一個簡單的HTTP請求。在這種HTTP請求的客戶端向服務器切換到WebSocket協議。)

1

你在瀏覽器中打開您的index.html不http://127.0.0.1:3434 它的一個WebSocket的服務器。您正試圖建立一個到websocket服務器的http連接。

相關問題