简体   繁体   English

客户端(JS-浏览器)和服务器(PHP)通过 Web-Socket 通过 IP 通信

[英]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:这是我的尝试:

  1. Client JS side:客户端JS端:

var ws = new WebSocket("ws://myIp:8090"); var ws = new WebSocket("ws://myIp:8090");

  1. Server PHP side:服务器PHP端:

define('HOST_NAME',"myIP");定义('HOST_NAME',"myIP");
define('PORT',"8090");定义('端口',“8090”);

Now in the browser when I go for,现在在浏览器中,当我去,

  1. 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 并成功建立通信。

  1. 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 套接字进行通信的方法。

  1. If the server and Client are in the same PC: The case 1 will be worked from the above question.如果服务器和客户端在同一台 PC 上: case 1将从上述问题中得到解决。
  2. If the server and client are in the different PC's: In the Client(JS) side set the IP address to the Server PC IP will work.如果服务器和客户端在不同的 PC 上:在客户端(JS)端将 IP 地址设置为服务器 PC IP 将起作用。

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.

相关问题 通过网络套接字接收数据时 Blob 获取的问题 - Problems with Blob acquisition when receiving data through a web-socket 如何在没有 web-socket 的情况下从服务器监听事件 - How to listen the event from server without web-socket 如何使用 socket.io 在客户端(网络浏览器)中显示来自 node.js 服务器的实时数据? - How to use socket.io to display realtime data in the client (web browser) from the node.js server? 是否可以识别收到的网络套接字请求? - Is it possible to identify web-socket request on receive? Android应用程序与Web浏览器之间的套接字通信 - Socket communication between Android app and web browser 客户端上的Java服务器与Javascript之间的套接字通信 - Socket Communication between Java server on client and Javascript 客户端服务器持续通信PHP - client server constant communication PHP 通过浏览器的地理位置API获取客户端的位置坐标,还是通过服务器的IP地址解析? - Obtain a client's location coordinates through the browser's geolocation API, or resolve through IP address from the server? 如何为“ws”Web-Socket 模块创建自定义事件? - How to create custom events for 'ws' Web-Socket module? 为什么尝试建立网络套接字时连接被拒绝? - Why is connection refused when trying to establish a web-socket?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM