workerman message push

Keywords: PHP socket Linux curl

workerman's message push

  1. Linux system environment detection

       Linux system can use the following script to test whether the native PHP environment meets the requirements of WorkerMan.
       curl -Ss http://www.workerman.net/check.php | php
       If all the above scripts show ok, it means that the requirements of WorkerMan are met
    
  2. Download of Web MSG sender

       Download the web MSG sender framework url: https://www.workerman.net/download/senderzip
       Start: run php start.php start -d from the linux system cd to the framework directory
       Test: browser access port http://ip:2123 or http: / / Domain Name: 2123
    
  3. The specific implementation code, generally change the url, and then the port does not change, and then add the corresponding business logic:
class Pushmsg extends Controller
{
    public function index(){
        echo "<script src='http://cdn.bootcss.com/socket.io/1.3.7/socket.io.js'></script>
                <meta http-equiv='Access-Control-Allow-Origin' content='*' />
                <script>
                    // Connect to the server, change workerman.net:2120 to the domain name or ip of the actual deployment of Web MSG sender service
                    var socket = io('http://www.yaowaren.com:2120');
                    // uid can be the user id of its own website to push and count the number of people online for uid
                    uid = 123;
                    // Log in as uid after socket connection
                    socket.on('connect', function(){
                        socket.emit('login', uid);
                    });
                    // When the backend pushes a message
                    socket.on('new_msg', function(msg){
                        console.log('Message received:'+msg);
                    });
                    // When the backend pushes online data
                    socket.on('update_online_count', function(online_stat){
                        console.log(online_stat);
                    });
                </script>";
    }

    public function pushMsgs(){
        // Indicates to whom to push. Blank means to push to all online users
        $to_uid = "456";
        // Push url address, use your own server address
        $push_api_url = "http://www.yaowaren.com:2121/";
        $post_data = array(
           "type" => "publish",
           "content" => "This is Jackey Pushed test data456",
           "to" => $to_uid, 
        );
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $push_api_url );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data );
        curl_setopt ($ch, CURLOPT_HTTPHEADER, array("Expect:"));
        $return = curl_exec ( $ch );
        curl_close ( $ch );
        var_export($return);
    }
}

More relevant knowledge of workerman can be focused on:
Pay attention to reply workerman and acquire relevant knowledge.

Posted by morgann7 on Fri, 13 Dec 2019 11:58:22 -0800