鍍金池/ 問答/人工智能  Java  PHP/ laravel 事件廣播 卡在 event 無法被 console.log 打

laravel 事件廣播 卡在 event 無法被 console.log 打印出來 event 沒有觸發(fā)

1 這是socket服務(wù)代碼

clipboard.png

2 事件代碼

clipboard.png

3觸發(fā)事件

clipboard.png

利用redis 執(zhí)行socket服務(wù)端會(huì)有打印數(shù)據(jù)

一旦切換成事件觸發(fā) socket服務(wù)端的console打印不出數(shù)據(jù)

請(qǐng)各位大神幫小弟解析一番 謝謝!

回答
編輯回答
乞許

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

2018年3月15日 12:48
編輯回答
她愚我

可以參考一下
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);
    }
});

/**
 * 監(jiān)聽消息發(fā)布
 *
 * @param string subscrbed 訂閱的頻道
 * @param string channel 消息發(fā)布的頻道
 * @param string message 消息通知數(shù)據(jù)(json格式)
 */
redis.on('pmessage', function (subscrbed, channel, message) {
    console.log("訂閱規(guī)則:" + subscrbed, "通知頻道:" + channel, "數(shù)據(jù):" + message);
    try {
        message = JSON.parse(message);
    } catch(e) {
        console.log('消息格式不正確,數(shù)據(jù)項(xiàng)請(qǐng)返回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';
    }

    /**
     * 設(shè)置通知數(shù)據(jù)
     *
     * @return array
     */
    public function broadcastWith() {
        $title = "新客戶通知";
        $content = $this->admin->name . "指派客戶【" . $this->custorm->name . "】給您,請(qǐng)及時(shí)跟進(jìn)處理。" ;
        return [
            "title" => $title,
            "content" => $content,
        ];
    }
}

JS

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

觸發(fā)

event(new MemberAssignNotification($custorm, $user_id));
2018年2月15日 16:25