(急)怎么把前端推送改成php推送

ssh1688

用前端推送可以成功,想改成通过push.php中转一下
 push.php推送端

<?php
/**
 * 认真编码 快乐生活
 * User: helloJiu
 * Date: 2018/9/12
 * Time: 18:06
 */
// 建立socket连接到内部推送端口

$client = stream_socket_client('tcp://127.0.0.1:1234', $errno, $errmsg, 1);

$data = array('cmd'=>'add_group', 'group_id'=>'123','message'=>'oo');

fwrite($client, json_encode($data));

// 读取推送结果
echo fread($client, 8192);
fclose($client);

前端推送

ws = new WebSocket('wss://socket.a.cn/wss');
ws.onmessage = function(data){console.log(data.data)};
ws.onopen = function() {
    ws.send('{"cmd":"add_group", "group_id":"123"}');
    ws.send('{"cmd":"send_to_group", "group_id":"123", "message":"哈哈哈1"}');
};

服务端start.php

<?php
    require_once './Workerman/Autoloader.php'; 
    require_once './Workerman/Lib/Timer.php';
    require_once './channel/src/Server.php';
    require_once './channel/src/Client.php';
    use Workerman\Worker;

    $channel_server = new Channel\Server('0.0.0.0', 2206);

    $worker = new Worker('websocket://0.0.0.0:1234');

    $worker->count = 8;
    // 全局群组到连接的映射数组
    $group_con_map = array();
    $worker->onWorkerStart = function(){
        // Channel客户端连接到Channel服务端
        Channel\Client::connect('127.0.0.1', 2206);

        // 监听全局分组发送消息事件
        Channel\Client::on('send_to_group', function($event_data){
            $group_id = $event_data;
            $message = $event_data;
            global $group_con_map;
            var_dump(array_keys($group_con_map));
            if (isset($group_con_map)) {
                foreach ($group_con_map as $con) {
                    $con->send($message);
                }
            }
        });
    };
    $worker->onMessage = function($con, $data){
        // 加入群组消息{"cmd":"add_group", "group_id":"123"}
        // 或者 群发消息{"cmd":"send_to_group", "group_id":"123", "message":"这个是消息"}
        $data = json_decode($data, true);
        var_dump($data);
        $cmd = $data;
        $group_id = $data;
        switch($cmd) {
            // 连接加入群组
            case "add_group":
                global $group_con_map;
                // 将连接加入到对应的群组数组里
                $group_con_map = $con;
                // 记录这个连接加入了哪些群组,方便在onclose的时候清理group_con_map对应群组的数据
                $con->group_id = isset($con->group_id) ? $con->group_id : array();
                $con->group_id = $group_id;
                break;
            // 群发消息给群组
            case "send_to_group":
                // Channel\Client给所有服务器的所有进程广播分组发送消息事件
                Channel\Client::publish('send_to_group', array(
                    'group_id'=>$group_id,
                    'message'=>$data
                ));
                break;
        }
    };
    // 这里很重要,连接关闭时把连接从全局群组数据中删除,避免内存泄漏
    $worker->onClose = function($con){
        global $group_con_map;
        // 遍历连接加入的所有群组,从group_con_map删除对应的数据
        if (isset($con->group_id)) {
            foreach ($con->group_id as $group_id) {
                unset($group_con_map);
            }
            if (empty($group_con_map)) {
                unset($group_con_map);
            }
        }
    };

    Worker::runAll();
2391 1 0
1个回答

walkor
require_once __DIR__ . '/Channel/src/Client.php';
Channel\Client::connect('127.0.0.1', 2206);
Channel\Client::publish('send_to_group', $event_data);
年代过于久远,无法发表回答
🔝