HTML5 / PHP WebSocket连接失败:WebSocket握手期间出错:意外响应代码:200

时间:2018-02-09 11:19:37

标签: php html5 websocket phpwebsocket

我认为有些人喜欢按压"这个问题没有表现出任何研究成果;它不清楚或没有用#34;按钮。我真的不知道他们想要证明什么?

如果您认为或相信此问题没有任何意义,请在进行计算前先做出评论

我正在尝试从浏览器连接到我的PHP套接字服务器。这是我得到的错误。

我确实检查了stackoverflow中的所有不同帖子,但没有任何帮助。

当我使用WS时。它工作正常。当我使用WSS时,它无法正常工作。

" WebSocket连接失败:WebSocket握手期间出错:意外响应代码:200"

这是我的apache配置

ServerName 192.168.56.106
ProxyRequests off

ProxyPass "/ws/"   "ws://localhost:9090/ws"
ProxyPass "/wss/"  "wss://localhost:9090/wss"

JS

function socketClient(vSocketIdentifications) { 
   console.log("I AM IN");
var $cHost  = "192.168.56.106";
var $cPort  = 9090;

//  this.wsUri = "wss://" + $cHost +":" + $cPort;

 /*
  * ENABLE THIS WHEN IN PRODUCTIONS
  * 
  */
if (window.location.hostname == $cHost){
    this.wsUri = "ws://" + $cHost + ":" + $cPort;
  } else {
    this.wsUri = ((window.location.protocol === "https:") ? "wss://" : 
"ws://") +  window.location.hostname + ":" + $cPort;
    }


  this.socket = "";


//create a new WebSocket object.  
this.socket = new WebSocket(this.wsUri);    

/*Let socket know who you are?*/
  var msg = {
  msgFrom: vSocketIdentifications,
  msg: "Hi",        
  msgClient: "js"
  };  

  this.socket.onopen = () =>  this.socket.send(JSON.stringify(msg));

  // Send text to all users through the server
    function sendInitMsgToServer() {
      // Construct a msg object containing the data the server needs to 
 process the message from the chat client.
    var msg = {
     msgFrom: vSocketIdentifications,
     msg: "Hi"   
    };

    // Send the msg object as a JSON-formatted string.
     this.socket.send(JSON.stringify(msg));

  }


//#### Message received from server?
this.socket.onmessage = function(ev) {
  var msg = JSON.parse(ev.data); //PHP sends Json data
  console.log(msg);
  var type = msg.type; //message type
  var umsg = msg.message; //message text
  var uname = msg.name; //user name
  var ucolor = msg.color; //color

  if(type == 'usermsg') 
  {
    //$('#message_box').append("<div><span class=\"user_name\" style=\"color:#"+ucolor+"\">"+uname+"</span> : <span class=\"user_message\">"+umsg+"</span></div>");
  if(umsg!=""){
   document.getElementById("information").innerHTML=umsg+"<br/>";
  }else{
    document.getElementById("progress").style.display="none";
    document.getElementById("information").innerHTML="I am done . What Next ? "+"<br/>";
  }
}  
};

this.closeSocket = function() {
  this.socket.close();
};

};

1 个答案:

答案 0 :(得分:1)

我找到了问题并解决了它。这是问题所在,在我的ProxyPass中,我把wss传给了wss。在我的情况下,它假设是wss ws。

我的旧代码(错误的代码)是

ProxyPass "/wss/"  "wss://localhost:9090/wss"

我的新代码(正确的代码)是

ProxyPass "/wss"  "ws://localhost:9090"