How does H5 WebSocket establish a connection with the server? It is difficult to find several examples on the Internet. How does H5 WebSocket establish a connection with the server? It is difficult to find several examples on the Internet, what should I do?
Reply content:
How does H5 WebSocket establish a connection with the server? I have found several examples on the Internet, but what should I do?
What I personally understand is:
1. Understand the websocket protocol and request specifications;
2. Use the PHP socket function library to process websocket requests;
3. Process websocket data frames.
The following is a small example I have written. I think the comments in it are quite detailed. I have not completed frame processing. I have processed more than 41 bytes of content, to learn about various word processing... To be supplemented.
Https://github.com/zhenbianshu/websocket
It should be easy to understand these basic applications. However, we recommend using mature extended workerman or swoole for projects.
If it is used, it depends on the configuration of your server,
Server: $ ws = new WebSocket ("127.0.0.1", "8080 ");
Client: var ws = new WebSocket ("ws: // FIG: 8080 ");
It doesn't matter where you put it... Run the server in one window to keep listening, and then run the client in another window to connect to the server.
This is a simple version. Message pushing is the same as HTTP response... The response is not forwarded again. The server returns the response directly, but the principle is the same.
You need a websocket service. You can use workman or swoole as the websocket service.
It is unrealistic to use traditional lnmp to implement socket.
I use node as a service.