duanqinbi9029 2018-02-09 11:19
浏览 1126
已采纳

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

I Think some people Just like to do press "This question does not show any research effort; it is unclear or not useful" Button. I really don't know what are they trying to prove?

If you think or believe this question does not make any sense, please do comment first before reach a calculation

I am trying to connect to my PHP socket server from the browser. This the error I am getting.

I did check all different post from stackoverflow, but nothing helped.

When I am using WS. it is working fine. When I am using WSS it is not working.

"WebSocket connection to failed: Error during WebSocket handshake: Unexpected response code: 200"

This is my apache Configuration

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条回答 默认 最新

  • douna2633 2018-02-16 11:55
    关注

    I found the issue and solved it. Here was the issue, in my ProxyPass I was passing wss to wss. It suppose to be wss to ws in my case.

    My Old code (Wrong one) was

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

    My New code (Correct one) is

    ProxyPass "/wss"  "ws://localhost:9090"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序