2012-08-30 57 views
0

我目前正在開始爲我的學士論文工作,並且最近開始'挖掘'到node.js和webSocket的使用中。在Firefox 15.0和Chrome 21.0.1180.89 m中訪問時,我的webSocket服務器運行時沒有問題。在Opera 12.02中,客戶端 - 服務器握手似乎存在問題。這是Opera的錯誤控制檯說:Opera 12中的WebSocket握手失敗(錯誤請求)

[31.08.2012 01:03:51] WebSockets - http://10.0.0.2/ 
Connection 
WebSocket handshake failure, invalid response code '400'. 

滑稽thgough:我不能在蜻蜓控制檯的網絡日誌中其他地方找到這個錯誤。訪問網站時請求的所有字段(index.html,client.js等)均按應有的方式找到並提供(HTTP狀態碼200 OK)。此外,我的服務器返回的唯一狀態代碼是200,404和500,所以這看起來像來自webSocket本身。

是的,WebSocket的IS在Opera中啓用了......我不知道這個問題可能是

什麼任何幫助將非常感激:)

編輯: 這是到目前爲止我的代碼,以便您可以看到哪些郵件頭。我的服務器發送和如何創建WebSocket連接與我client.js:

server.js:

// load required modules: 
var http = require("http"); 
var WebSocketServer = require("websocket").server; 
path = require("path"); 
url = require("url"); 
filesys = require("fs"); 

// declare listening port vars: 
var httpListeningPort = 80; 
var webSocketListeningPort = 80; 

// create HTTP-server: 
var httpSrv = http.createServer(function(request, response) { 
    console.log((new Date()) + ":\tReceived request for " + request.url); 
// response.writeHead(200); 
// response.end(); 
    var myPath = url.parse(request.url).pathname; 
    var fullPath = path.join(process.cwd(), myPath); 
    if(myPath === "/") { 
     fullPath += "index.html"; 
     console.log("Full path:\t" + fullPath); 
     filesys.readFile(fullPath, "binary", function(err, file) { 
      if(err) { 
       response.writeHeader(500, {"Content-Type": "text/plain"}); 
       response.write(err + "\n"); 
       response.end(); 
      } 
      else { 
       response.writeHeader(200); 
       response.write(file, "binary"); 
       response.end(); 
      } 
     }); 
    } 
    else { 
     path.exists(fullPath, function(exists) { 
     if(!exists) { 
      response.writeHeader(404, {"Content-Type": "text/plain"}); 
      response.write("404 Not Found\n"); 
      response.end(); 
     } 
     else { 
      filesys.readFile(fullPath, "binary", function(err, file) { 
       if(err) { 
        response.writeHeader(500, {"Content-Type": "text/plain"}); 
        response.write(err + "\n"); 
        response.end(); 
       } 
       else { 
        response.writeHeader(200); 
        response.write(file, "binary"); 
        response.end(); 
       } 
      }); 
     } 
     }); 
    } 
}); 
httpSrv.listen(httpListeningPort, function() { 
    console.log((new Date()) + ":\tServer is now listening on port " + httpListeningPort); 
}); 

// create webSocket server and tie it to the http server: 
wsServer = new WebSocketServer({ 
    httpServer: httpSrv 
}); 

function originChecker(origin) { 
    if(origin) { 
     console.log("Origin " + origin + " is allowed"); 
     return true; 
    } else { 
     console.log("origin is NOT allowed."); 
     return false; 
    } 
} 

// how to handle requests: 
wsServer.on("request", function(request) { 
    // check whether origin is allowed or not: 
    if(originChecker(request.origin) === false) { 
     request.reject(); 
     console.log((new Date()) + ":\tConnection request from origin " + request.origin + " rejected."); 
     return; 
    } 

    // accept the connecteion request -> open the connection: 
    var connection = request.accept(null, request.origin); 
    console.log((new Date()) + ":\tConnection request from " + request.origin + " accepted."); 

    // handle incoming messages from the clients: 
    connection.on("message", function(message) { 
     if(message.type === "utf8") { 
      console.log((new Date()) + ":\tReceived message from " + request.origin + ":\nType: " + message.type + "\nLength: " + message.utf8Data.length + "\nMessage: " + message.utf8Data); 
      // echo "Message received": 
      connection.sendUTF(JSON.stringify({ type: "message", data: "Message received !" })); 
     } else { 
      // send error message back to client: 
      console.log((new Date()) + ":\tReceived message from " + request.origin + ":\nERROR:\tMessage is NOT UTF-8! it's " + message.type); 
      connection.sendUTF(JSON.stringify({ type: "message", data: "ONLY UTF-8 accepted !" })); 
     } 
    }); 

    // what to do when connection is closed: 
    connection.on("close", function() { 
     console.log((new Date()) + ":\tClient @" + connection.remoteAddress + " disconnected."); 
    }); 
}); 

CL ient.js:

function client() { 

    if("WebSocket" in window) { 
     alert("WebSocket is supported by your browser!"); 

     // try to connect to the webSocket server: 
     var connection = null; 
     connection = new WebSocket("ws://10.0.0.2:80"); 

     // things to do once the connection is opened: 
     connection.onopen = function() { 
      alert("INFO:\tConnection to server is OPEN"); 

      document.getElementById("msgInput").focus(); 
      document.getElementById("msgInput").disabled = false; 
      document.getElementById("msgInput").value = ""; 

      document.getElementById("msgInput").onkeyup = function(key) { 
       switch(key.keyCode) { 
        case 13: if(document.getElementById("msgInput").value === "") { 
           break; 
           } 
           var messageText = document.getElementById("msgInput").value; 
           document.getElementById("msgInput").disabled = true; 
           document.getElementById("msgInput").value = ""; 
           document.getElementById("statusHeader").innerHTML = "Sending..."; 
           connection.send(messageText); 
        break; 
        default: document.getElementById("statusHeader").innerHTML = "Press ENTER to send!"; 
       } 
      }; 
     }; 

     connection.onerror = function(error) { 
      document.body.style.backgroundColor = "#220000"; 
      document.body.style.color = "#aa0000"; 
      document.getElementById("statusHeader").innerHTML = "ERROR connecting to server -> OFFLINE"; 
      return; 
     }; 

     connection.onmessage = function(message) { 
      try { 
       var json = JSON.parse(message.data); 
      } catch (error) { 
       alert("ERROR parsing message:\t" + error); 
       return; 
      } 

      document.getElementById("statusHeader").innerHTML = json.data; 
      document.getElementById("msgInput").disabled = false; 
     }; 

     connection.onclose = function() { 
      setTimeout(function() { 
      document.body.style.backgroundColor = "#808080"; 
      document.body.style.color = "#ffffff"; 
      document.getElementById("statusHeader").innerHTML = "OFFLINE"; 
      document.getElementById("msgInput").disabled = true; 
      document.getElementById("msgInput").value = "OFFLINE"; 
      }, 5000); 
      return; 
     }; 
    } else { 
     alert("WebSocket is NOT supported by your browser! Exiting now."); 
     return; 
    } 
}; 
+0

您能提供更多信息:您使用的是什麼服務器?你用什麼javascript創建連接?您的服務器返回握手失敗的標頭? – simonc

+0

嗨simonc,我編輯了我的問題,並添加了我的服務器和客戶端腳本的源代碼。正如你所看到的,atm我的服務器並沒有專門處理壞信號 - 我發送的唯一標題是404(如果找不到請求URI),500(如果打開請求的文件時出錯)(例如缺少文件許可等) )和200,如果一切順利的話。這只是令人奇怪的一點,即訪問服務器在Firefox和Chrome中起作用,但在Opera中,由於握手失敗,我得到了一個不好的請求,這對我來說並不合適。 「我在用哪​​個JS」是什麼意思? – slagjoeyoco

+0

謝謝。我剛剛注意到這個[前面的SO問題](http://stackoverflow.com/questions/11116515/running-web-socket-server-on-opera-12),它聲稱Opera 12只支持一箇舊的,棄用的版本的websockets。您現在可以堅持使用其他瀏覽器,並在Opera更新時再次使用Opera進行測試嗎? – simonc

回答

1

根據recent question歌劇院12支持WebSockets的較舊的,不兼容的版本。該版本(Hixie-76)在其握手中使用了一組不同的頭文件。你的服務器可能不理解這些解釋它的400錯誤響應。

如果您有能力等待Opera趕上,那麼最簡單的'解決方案'就是現在使用其他瀏覽器進行測試。 hixie協議草案已棄用,因此Opera將最終升級到RFC 6455。

+3

[Opera Next](http://www.opera.com/browser/next/)已經支持最終的RFC。 – gsnedders