Simple use of WebSocket

Source: Internet
Author: User

The principle of 1.websocket

Refer to this post for details http://blog.csdn.net/hhwell/article/details/44625287

2. Using the WebSocket interface in Workerman

Workerman official website http://www.workerman.net/

Suppose you now have a need to instantly update the list of people watching live

(1) First establish a mapping table with the current process ID and the live video ID, as well as the u_id (user ID) and u_name (user name) fields in the table.

(2) to the official website download Workerman, I environment is window, so I downloaded a window version of the

The introduction of this file in the project will enable you to use the

(4) Client establishes WebSocket connection

OnOpen: Triggered when the connection is successful

OnMessage: Triggered when a message is sent by the server

OnClose: triggered when disconnected

OnError: Triggered when connection error occurs

STR is a string to be transmitted to the server

(5) service-side code

The above code has been annotated, the process is that when the data sent by the client, processing the transmitted string, and then insert the data into the mapping table, when returning data to the client, first check whether there is a record in the database to watch the live broadcast, some words to get the user name, If not, then directly return to the current user (in fact, this is the best use of caching, such as redis processing, but in order to facilitate the description of the data in the database), when disconnected, according to the process ID to delete the records in the mapping table

(6) test

PHP start.php Start Service

Open Browser, Access test.html

Related test code Download:

Simple use of WebSocket

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.