Laravel 5.5 Redis + Socket.IO不会工作

时间:2018-03-09 05:52:25

标签: php laravel redis

我已经在这里工作了几个小时,我无法解决这个问题。所以我使用 Laravel 5.5 ,我的 PHP版本为7 。现在我的项目已经完成,但我想在用户之间添加实时通信。我试图测试一些东西,但它只是不起作用。我的本地开发环境是MAC,我使用Laravel Valet来为我的网站服务。

我遵循了本教程:Tutorial

首先我安装了Redis& Socket.io如下:

$ composer require predis/predis
$ npm install express ioredis socket.io --save

然后我使用brew安装了redis服务器,然后启动它

redis-server --port 3001

现在我按照以下方式制作了我的活动(App \ Events \ NewUserSignup):

<?php

namespace App\Events;

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

class NewUserSignup implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $username;

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

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        \Log::info('Event fired');
        return new Channel('test-channel');
    }
}

然后我在根目录中创建了我的Socket.js(Root)文件,如下所示:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Redis = require('ioredis');
var redis = new Redis();
redis.subscribe('test-channel', function(err, count) {
});
redis.on('message', function(channel, message) {
    console.log('Message Recieved: ' + message);
    message = JSON.parse(message);
    io.emit(channel + ':' + message.event, message.data);
});
http.listen(3000, function(){
    console.log('Listening on Port 3000');
});

然后我在web.php文件中添加了两条这样的新路由:

Route::get('/broadcasting/fire', function(){
   event(new App\Events\NewUserSignup("james"));
   return "Event fired";
});

Route::get('/broadcasting/test', function(){
    return view('broadcasting_test');
});

然后我按照以下方式创建了我的视图(Resources \ views \ broadcasting_test):

<meta name="csrf-token" content="{{ csrf_token() }}">

<div class="container">
    <h1 id="addition">0</h1>
</div>

<script src="{{ asset('js/jquery.js') }}" type="application/javascript" language="JavaScript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js"></script>
<script>
    var socket = io('http://localhost:3000');
    socket.on("test-channel:App\\Events\\NewUserSignup", function(message) {
        console.log('here');
        $('#addition').text(parseInt($('#addition').text()) + parseInt(message.data.addition));
    });
</script>

所以我遵循了我应该做的一切。我开始了每一步,甚至使用

启动套接字服务器
node socket.js

然后我启动了redis服务器,但每当我重新启动触发页面时都没有任何反应!我的终端没有任何改变,只是一无所获。我做错了什么?

1 个答案:

答案 0 :(得分:0)

您的配置似乎是正确的..我尝试了同样的事情,它确实有效,只是为了确保您在.env文件中设置php artisan config:clear后运行BROADCAST_DRIVER=redis