Here I draw a diagram showing the handshake part of the WebSocket connection between the client and the server, which can be done very easily in node, because the net module provided by node has already encapsulated the socket socket, and developers only need to use Consider the interaction of the data without having to deal with the establishment of the connection. and PHP does not, from the socket connection, establishment, binding, monitoring, and so on, these need our own to operate, so it is necessary to come out and say.
+--------+ 1. Send Sec-websocket-key +---------+ | |--------------------------------> | | | | 2. Encrypt back sec-websocket-accept | | | client | <--------------------------------| server | | | 3. Local calibration | | | |--------------------------------> | | +--------+ +--------+
Read the last article I wrote the classmate should be to have a more comprehensive understanding. ① and ② are actually an HTTP request and response, but we're dealing with a string that's not parsed. Such as:
Get/chat http/1.1host:server.example.comorigin:http://example.com
The request we always see is this, and when this thing is on the server side, we can get the information directly from some code base.
First, the PHP processing WebSocket
WebSocket connections are initiated by the client, so everything starts from the client. The first step is to parse the Sec-websocket-key string that gets sent to the client.
Get/chat Http/1.1host:server.example.comupgrade:websocketconnection:upgradesec-websocket-key: Dghlihnhbxbszsbub25jzq==origin:http://example.comsec-websocket-protocol:chat, Superchatsec-websocket-version:13
The format of the client request (above) is also mentioned in the previous article, and first PHP establishes a socket connection to listen for information on the port.
1. Setting up the socket connection
On the establishment of socket sockets, I believe many universities have built computer network of people know, the following is a link to establish the process:
// Create a socket socket $master = socket_create (Af_inet, Sock_stream, sol_tcp); Socket_set_option ($master, Sol_ SOCKET, SO_REUSEADDR, 1); Socket_bind ($master$address$port); socket_listen ($master);
Compared to node, this place is too cumbersome to handle, the above lines of code are not connected, but the code is to create a socket socket must write something. Because of the complexity of the process, I wrote all sorts of processing into a class that was easy to manage and invoke.
//demo.phpClassWS {var $master;//client connecting to server var $sockets=Array();//socket management in different states var $handshake=false;//Judging whether to shake hands function__construct ($address,$port){ //Create a socket socket $this->master = Socket_create (Af_inet, Sock_stream,sol_tcp) or die("Socket_create () failed"); Socket_set_option ($this->master, Sol_socket, SO_REUSEADDR, 1) or die("Socket_option () failed"); Socket_bind ($this->master,$address,$port) or die("Socket_bind () failed"); Socket_listen ($this->master, 2) or die("Socket_listen () failed"); $this->sockets[] =$this-master; //Debug Echo("Master socket:".)$this->master. " \ n "); while(true) { //automatically select the socket for the message if the handshake is automatically selected by the host $write=NULL; $except=NULL; Socket_select ($this->sockets,$write,$except,NULL); foreach($this->sockets as $socket) { //client connecting the host if($socket==$this-Master) { $client= Socket_accept ($this-master); if($client< 0) { //Debug Echo"Socket_accept () failed"; Continue; } Else { //Connect ($client); Array_push($this->sockets,$client); Echo"Connect client\n"; } } Else { $bytes= @socket_recv ($socket,$buffer, 2048,0); if($bytes= = 0)return; if(!$this-handshake) { //If there is no handshake, shake hands first to respond to//dohandshake ($socket, $buffer); Echo"Shakehands\n"; } Else { //If you have already shook hands, accept the data directly and handle $buffer= Decode ($buffer); //process ($socket, $buffer); Echo"Send file\n"; } } } } }}
The above code is through my debugging, not too much problem, if you want to test, you can type in the cmd command line php /path/to/demo.php
; Of course, the above is just a class, if you want to test, you have to create a new instance.
$ws New WS (' localhost ', 4000);
The client code can be a little simpler:
var New WebSocket ("ws://localhost:4000"function() { console.log ("handshake succeeded" function() { console.log ("error");};
Run the server code, when the client connects, we can see:
Through the above code can clearly see the whole process of communication. The first is to establish a connection, which in node has been encapsulated in the net and HTTP modules, and then determine whether to shake hands, if not, shakehands. Here's the handshake I directly echo a word, indicating that this thing, the previous article we mentioned the handshake algorithm, here is directly written.
2. Extracting Sec-websocket-key Information
Turn https://www.cnblogs.com/hustskyking/p/websocket-with-php.html
Elaborate websocket-php (not finished)