[英]Client(JS-Browser) and Server(PHP) communication over IP through Web-Socket
I am new to web-sockets.我是网络套接字的新手。 I am trying to establish communication between client and server through web-socket.我正在尝试通过网络套接字在客户端和服务器之间建立通信。 I follow the code from this link:我按照此链接中的代码进行操作:
https://phppot.com/php/simple-php-chat-using-websocket/ https://phppot.com/php/simple-php-chat-using-websocket/
Here is my try:这是我的尝试:
var ws = new WebSocket("ws://myIp:8090"); var ws = new WebSocket("ws://myIp:8090");
define('HOST_NAME',"myIP");定义('HOST_NAME',"myIP");
define('PORT',"8090");定义('端口',“8090”);
Now in the browser when I go for,现在在浏览器中,当我去,
http://localhost:8090
: The initial handshake Request Headers in browser: http://localhost:8090
:浏览器中的初始握手请求标头:Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.9 Cache-Control: no-cache Connection: Upgrade Host: myIP:8090 Origin: http://localhost:8090 Pragma: no-cache Sec-WebSocket-Extensions: permessage-deflate; Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.9 Cache-Control: no-cache Connection: Upgrade Host: myIP:8090 Origin: http://localhost:8090 Pragma: no-cache Sec -WebSocket-Extensions:permessage-deflate; client_max_window_bits Sec-WebSocket-Key: tJ0fWdCEfJCUcmtRCNOJuQ== Sec-WebSocket-Version: 13 Upgrade: websocket User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36 client_max_window_bits Sec-WebSocket-Key: tJ0fWdCEfJCUcmtRCNOJuQ== Sec-WebSocket-Version: 13 升级: websocket User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36
And Response headers in PHP Server is: PHP Server 中的响应头是:
HTTP/1.1 101 Web Socket Protocol Handshake Upgrade: websocket Connection: Upgrade WebSocket-Origin: myIP WebSocket-Location: ws://myIP:8090/demo/shout.php Sec-WebSocket-Accept:DqaK+Z+jqf1lJ0vj5bIxFi7+EPc= HTTP/1.1 101 Web Socket协议握手升级:websocket连接:升级WebSocket-Origin:myIP WebSocket-Location:ws://myIP:8090/demo/shout.php Sec-WebSocket-Accept:DqaK+Z+jqf1lJ0vj5bIxFi7+EPc=
As in above both request and response headers upgrade their protocol to web-socket and communication establish successfully.如上所述,请求和响应标头都将它们的协议升级为 web-socket 并成功建立通信。
http://myIP:8090
: http://myIP:8090
:Request Headers are in browser:请求标头在浏览器中:
Provisional headers are shown Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.9 Cache-Control: no-cache Connection: Upgrade Host: myIP:8090 Origin: http://myIP:8090 Pragma: no-cache Sec-WebSocket-Extensions: permessage-deflate;显示临时标头 Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.9 Cache-Control: no-cache Connection: Upgrade Host: myIP:8090 Origin: http://myIP:8090 Pragma:无缓存 Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits Sec-WebSocket-Key: D73LNcTLB8P6dzPilGipQQ== Sec-WebSocket-Version: 13 Upgrade: websocket User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36 client_max_window_bits Sec-WebSocket-Key: D73LNcTLB8P6dzPilGipQQ== Sec-WebSocket-Version: 13 升级: websocket User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36
but the PHP server receives the Request Headers are:但是 PHP 服务器收到的请求标头是:
GET / HTTP/1.1 Host: localhost:8090 Connection: keep-alive Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36 Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng, / ;q=0.8,application/signed-exchange;v=b3;q=0.9 Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.9 If-None-Match: W/"41e-5e6jpMk0FNkw+DjhkfcoeaiAhE4" GET / HTTP/1.1 Host: localhost:8090 Connection: keep-alive Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0 .4280.66 Safari浏览器/ 537.36接受:text / html的,应用/ XHTML + xml的,应用/ XML; q = 0.9,图像/ avif,图像/ WEBP,图像/ APNG,/ q = 0.8,应用/签署交换; v =b3;q=0.9 Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.9 If-None-Match: W/"41e-5e6jpMk0FNkw+DjhkfcoeaiAhE4"
As seen above the PHP Server does not receive the web-socket upgrade headers sent by the browser.如上所示,PHP 服务器不会收到浏览器发送的 web-socket 升级标头。 So the server does not receive any Sec-WebSocket-Key
in request headers and got this error:所以服务器在请求头中没有收到任何Sec-WebSocket-Key
并得到这个错误:
ErrorException: Undefined index: Sec-WebSocket-Key
while creating the response headers.在创建响应标头时。 I dont know how this can be tackled?我不知道如何解决这个问题? How does the Request Headers are getting modified?请求标头如何被修改? Have I done something wrong in the setup?我在设置中做错了什么吗? And can this setup work to establish the communication on two different PC's?此设置是否可以在两台不同的 PC 上建立通信? Please Help.请帮忙。 Thanks in Advance!提前致谢!
After some try here and there I found the way to communicate through Web-sockets in the same or different PC's.经过在这里和那里的一些尝试后,我找到了通过相同或不同 PC 中的 Web 套接字进行通信的方法。
case 1
will be worked from the above question.如果服务器和客户端在同一台 PC 上: case 1
将从上述问题中得到解决。Client JS side:客户端JS端:
var ws = new WebSocket("ws://Server_PC_IP:8090"); var ws = new WebSocket("ws://Server_PC_IP:8090");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.