Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement WebSocket with PHP

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how PHP implements WebSocket, Xiaobian feels quite practical, so share it with you as a reference, I hope you can gain something after reading this article.

What is WebSocket?

Excerpt some explanations online:

WebSocket protocol is a new network protocol based on TCP. It enables browser-server full-duplex communication-allowing the server to actively send information to the client.

The WebSocket communication protocol was defined as RFC 6455 by the IETF in 2011 and supplemented by RFC7936.

--Baidu Encyclopedia

WebSocket is a persistent protocol, as opposed to http, which is non-persistent.

For a simple example, http1.0's lifecycle is defined by a request, that is, a request, a response, for http, the client and server session ends here; and in http1.1, there is a slight improvement, that is, keep-alive is added, that is, multiple request requests and multiple response acceptance operations can be performed in an http connection. However, in real-time communication, it does not have much effect. HTTP can only be requested by the client before the server can return information, that is, the server cannot actively push information to the client, and cannot meet the requirements of real-time communication. WebSocket can make persistent connection, that is, the client only needs to shake hands once, and after success, data communication can be continued. It is worth noting that WebSocket realizes full-duplex communication between client and server, that is, when there is data update on the server side, it can be actively pushed to the client side.

The image above demonstrates a handshake between client and server when establishing a WebSocket connection

Information requested by client from server when establishing WebSocket

GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket //tells the server that it is now sending WebSocket Protocol Connection: Upgrade Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw== //is a Base64 encode value randomly generated by the browser to verify whether the data returned by the server is WebSocket assistant Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 13 Origin: http://example.com

After obtaining the information requested by the client, the server processes the data according to the WebSocket protocol and returns it, in which operations such as encryption of Sec-WebSocket-Key are required.

HTTP/1.1 101 Switching Protocols Upgrade: websocket //is still fixed, telling clients that an upgrade is coming is the Websocket protocol, not mozillasocket, lurnarsocket or shitsocket Connection: Upgrade Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk= //This is the Sec-WebSocket-Key that has been confirmed and encrypted by the server, that is, the credential that the client requires to establish WebSocket verification Sec-WebSocket-Protocol: chatPHP server

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report