As a socket persistent connection server, if Swoole wants to actively push a message to the user whose uid is 123, how should it be written?

Source: Internet
Author: User
Well, this should be a problem at the Cainiao level, but I still have no idea if I have read the documentation. Well, this should be a problem at the Cainiao level, but I still have no idea if I have read the documentation.

Reply content:

Well, this should be a problem at the Cainiao level, but I still have no idea if I have read the documentation.

Php socket is a resource type:
Resource (4) of type (Socket), that is, each socket handle looks like ..

Linux C socket fd is an int integer, each of which is a different number.

Native php cannot "actively push a message to the user whose uid is 123 already connected ",
However, with Swoole using the socket implemented by linux C, we can "actively push a message to the user whose uid is 123 already connected ".

Here we can use the simplest share memory of System V to share data IPC between processes and maintain a ing table between user IDs and socket connections.

The code is as follows:


  Set (array ('worker _ num' => 8, // number of worker processes 'daemonize '=> false, // whether it is used as a daemonize process )); $ shmid = shm_attach (getmypid (), 1024*66); # allocate 66kB for shared memorydefine ("SHARED_UID_CONNECTION_MAP", 12); shm_put_var ($ shmid, timeout, []); $ serv-> on ('connect', function ($ serv, $ fd) use ($ shmid) {echo "Client: connect. \ n ";}); $ serv-> on ('receive ', function ($ serv, $ fd, $ from_id, $ data) use ($ shmid) {$ serv-> send ($ fd, 'swoole :'. $ data); // $ serv-> close ($ fd); $ trimdata = trim ($ data); if ($ trimdata = 'I am lucky man ') {$ map = shm_get_var ($ shmid, SHARED_UID_CONNECTION_MAP); $ map [] = $ fd; # $ map [$ uidFromData] = $ fd; shm_put_var ($ shmid, SHARED_UID_CONNECTION_MAP, $ map);} if ($ trimdata = 'broadcast to lucky man') {$ map = shm_get_var ($ shmid, SHARED_UID_CONNECTION_MAP); // var_dump ($ map ); $ map is array whose valuse is int $ fd foreach ($ map as $ fd) {$ serv-> send ($ fd, "some body broadcast to you luck man") ;}# broadcast to all users // $ start_fd = 0; // while (true) // {// $ conn_list = $ serv-> connection_list ($ start_fd, 10); // if ($ conn_list = false or count ($ conn_list) === 0) // {// echo "finish \ n"; // break; //} // $ start_fd = end ($ conn_list ); // var_dump ($ conn_list); // foreach ($ conn_list as $ fd) // {// $ serv-> send ($ fd, "broadcast "); ///} // var_dump ($ serv-> connections); // foreach ($ serv-> connections as $ k => $ v) {// $ lk = [$ k, $ v]; // var_dump ($ lk); //}); $ serv-> on ('close ', function ($ serv, $ fd) {echo "Client: Close. \ n ";}); $ serv-> start ();

Then you can open n clients to test the code. for example, I open 3 telnet 127.0.0.1 1024 and send the last message to the server "I am lucky man ", the first one sends "broadcast to lucky man" to the server, and the third one receives "some body broadcast to you luck man" to send messages to specific users.

It's that simple,
The Swoole method is good, and asynchronous io + multi-process is also the most mature Final Solution (pay tribute to Nginx)
Hahaha

Uid is only the uid you recognize. the socket link to the service has a unique id. you must send messages to the client using this unique id, you can map the two.

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.