laravel 事件广播 卡在 event 无法被 console.log 打印出来 event 没有触发

1 这是socket服务代码

clipboard.png

2 事件代码

clipboard.png

3触发事件

clipboard.png

利用redis 执行socket服务端会有打印数据

一旦切换成事件触发 socket服务端的console打印不出数据

请各位大神帮小弟解析一番 谢谢!

阅读 3.8k
2 个回答

QUEUE_DRIVER=redis造成的,把QUEUE_DRIVER=redis改成sync就好啦

可以参考一下
nodejs

// server.js
var app = require('http').createServer(handler);
var io = require('socket.io')(app);

var Redis = require('ioredis');
var redis = new Redis(6379, '192.168.0.203');

app.listen(6001, function () {
    console.log('Socket server is running at port 6001!');
});

function handler(req, res) {
    res.writeHead(200);
    res.end('');
}

io.on('connection', function (socket) {
    socket.on('message', function (message) {
        console.log(message)
    });
    socket.on('disconnect', function () {
        console.log('user disconnect')
    });
});

/**
 * 频道订阅
 *
 * 订阅order频道 (支持正则匹配如:order.*)
 */
redis.psubscribe('*', function (err, count) {
    if(err) {
        console.log("订阅失败",err);
    }
});

/**
 * 监听消息发布
 *
 * @param string subscrbed 订阅的频道
 * @param string channel 消息发布的频道
 * @param string message 消息通知数据(json格式)
 */
redis.on('pmessage', function (subscrbed, channel, message) {
    console.log("订阅规则:" + subscrbed, "通知频道:" + channel, "数据:" + message);
    try {
        message = JSON.parse(message);
    } catch(e) {
        console.log('消息格式不正确,数据项请返回JSON格式字符串');
        return false;
    }

    io.emit(channel + ':' + message.event, message.data);
});

PHP

class MemberAssignNotification implements ShouldBroadcast {

    protected $user_id;
    protected $custorm;
    protected $admin;

    public function __construct(Custorm $custorm, $user_id = 0)
    {
        $this->custorm= $custorm;
        $this->user_id = $user_id;
        $this->admin = auth('admin')->user();
    }

    public function broadcastOn() {
        return [new Channel('member.' . $this->user_id)];
    }

    /**
     * @return string
     */
    public function broadcastAs() {
        return 'assign.notification';
    }

    /**
     * 设置通知数据
     *
     * @return array
     */
    public function broadcastWith() {
        $title = "新客户通知";
        $content = $this->admin->name . "指派客户【" . $this->custorm->name . "】给您,请及时跟进处理。" ;
        return [
            "title" => $title,
            "content" => $content,
        ];
    }
}

JS

 // 客户指派提醒消息
    socket.on("member." + nowUid + ":assign.notification", function (data) {
        toastr.info(data.content, data.title, {
            "timeOut": 14400000,
            "extendedTimeOut": 14400000
        });
    });

触发

event(new MemberAssignNotification($custorm, $user_id));
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题