swoole实现websocket消息主题订阅及推送服务代码

代码语言:phpcli

所属分类:通讯

代码描述:swoole实现websocket消息主题订阅及推送服务代码,html代码链接:https://blog.bfw.wiki/user1/16531104623698250077.html

代码标签: swoole websocket 消息 主题 订阅 推送 服务

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<?php
$_client_fd = [];

$_topic_list = [];
$server = new Swoole\Websocket\Server("0.0.0.0", 9502);

$server->on('open', function($server, $req) {
    echo "connection open: {$req->fd}\n";
    global $_client_fd;
    $_client_fd[] = $req->fd;
});


$server->on('message', function($server, $frame) {


    $_jsondata = json_decode($frame->data, true);
    if (isset($_jsondata['type'])) {
        if ($_jsondata['type'] == 'heart') {
            $server->push($frame->fd, $frame->data);
            return;
        }

        if ($_jsondata['type'] == 'subs' && isset($_jsondata['name'])) {

            global $_topic_list;
            print_r($_topic_list);
            if (isset($_topic_list[$_jsondata['name']])) {
                if (!in_array($frame->fd, $_topic_list[$_jsondata['name']])) {
                    echo "订阅成功\n";
                    $_topic_list[$_jsondata['name']][] = $frame->fd;
                }
            } else {
                echo "订阅成功\n";
                $_topic_list[$_jsondata['name']][] = $frame->fd;
            }
            print_r($_topic_list);


        }

        if ($_jsondata['type'] == 'unsubs' && isset($_jsondata['name'])) {

            global $_topic_list;
            print_r($_topic_list);
            print_r($frame->fd);
            if (isset($_topic_list[$_jsondata['name']])) {
                foreach ($_topic_list[$_j.........完整代码请登录后点击上方下载按钮下载查看

网友评论0