How to establish a connection between javascript-php and websocket

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.