0

这里是事件

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class FieldWasUpdated implements ShouldBroadcast
{
    use InteractsWithSockets, SerializesModels;
    public $instance;
    public $key;
    public $value;

/**
 * Create a new event instance.
 *
 * @return void
 */
public function __construct($instance, $key, $value)
{
    $this->instance = $instance;
    $this->key = $key;
    $this->value = $value;
}

/**
 * Get the channels the event should broadcast on.
 *
 * @return Channel|array
 */
public function broadcastOn()
{
    return new Channel("model." . $this->instance->getModelType() . "." . $this->instance->id);
}


    //public function broadcastAs() {
    //    return "FieldWasUpdated"; commented out for testing
    //}
}

这是前端代码

 window.Echo = new window.LaravelEcho({
            broadcaster: 'socket.io',
            host: window.location.hostname + ':3000'
        });
        // I use different types of `listen` for testing
        Echo.channel("model.ANNOUNCEMENT.2")
            .listen(".*", function(e) {
                console.log(e);
            })
            .listen("*", function(e) {
                console.log(e);
            })
            .listen("FieldWasUpdated", function(e) {
                console.log(e);
            })
            .listen(".FieldWasUpdated", function(e) {
                console.log(e);
            })

node.js 代码

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Redis = require('ioredis');
var redis = new Redis();


http.listen(3000, function(){
console.log('Listening on Port 3000');
});

redis.psubscribe('*', function(err, count) {
console.log(err, count);
});
redis.on('pmessage', function(subscribed, channel, message) {
    // different types of emit for testing
    message = JSON.parse(message);
    const ev = channel + ":" + message.event;
    io.emit(ev, message.data);
    io.emit(channel, message);
    io.emit(message, channel);
    io.emit(channel, message.event, message.data);
    io.emit(channel, message);
    io.emit(channel, message.data, message.event);
});

这是我在 laravel 应用程序中触发 Event 时在 chrome 开发工具中看到的内容 在此处输入图像描述

所以 websocket 正在工作,但没有调用任何处理程序。

当我取消注释 broadcastAs 功能时,什么都没有改变。

当我将频道重命名为test-channel后端和前端时 - 没有任何变化。

4

1 回答 1

0

一段时间后找到了解决方案:

服务器:

redis.psubscribe('*', function(err, count) {
    console.log(err, count);
});
redis.on('pmessage', function(subscribed, channel, message) {
    message = JSON.parse(message);
    io.emit(message.event, channel, message.data);
});

客户

// both handlers process same event
Echo.channel("model.ANNOUNCEMENT.2")
            .listen("FieldWasUpdated", function(e) {
                console.log(e);
            })
            .listen(".App.Events.FieldWasUpdated", function(e) {
                console.log(e);
            })
于 2017-02-06T20:25:03.757 回答