html5 websocke c,html - HTML5 / PHP WebSocket connection to failed: Error during WebSocket handshake...

本文探讨了一种特定的WebSocket连接失败的情况,错误信息为“WebSocket连接到...:错误发生在WebSocket握手阶段:意外的响应代码:200”。作者尝试通过配置Apache代理来解决此问题,并分享了使用WS和WSS协议时遇到的不同结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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("

"+uname+" : "+umsg+"
");

if(umsg!=""){

document.getElementById("information").innerHTML=umsg+"
";

}else{

document.getElementById("progress").style.display="none";

document.getElementById("information").innerHTML="I am done . What Next ? "+"
";

}

}

};

this.closeSocket = function() {

this.socket.close();

};

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值