2017-09-28 210 views
2

我試圖顯示用戶登錄到網站時的實時消息通知計數。爲此,我正在使用socketio,redis和laravel事件。但它不起作用。在laravel日誌中,我收到一條消息。在此消息中顯示count,但socket爲空。我想我錯過了什麼。我的gitbash顯示頻道訂閱和用戶連接。Laravel事件廣播與socketio和redis不在laravel 5.4中工作?

Gitbash

[email protected]:~/Code/cabinapi$ node socket.js 
    listening on *:3000 
    Redis: message-channel subscribed 
    a user connected 

Laravel登錄

[2017-09-28 07:21:57] local.INFO: Broadcasting [App\Events\MessageEvent] on channels [message-channel] with payload: 
{ 
    "count": 9, 
    "socket": null 
} 

控制器

public function privateMessageAPICount($id) 
{ 
     $count = PrivateMessage::where('receiver_id', new \MongoDB\BSON\ObjectID($id)) 
      ->where('read', 0) 
      ->count(); 

     if ($count > 0){ 
      event(new MessageEvent($count)); 
     } 
} 

routes-> web.php

Route::get('/message/count/{id}', 'Cabinowner\[email protected]'); 

.ENV

APP_URL=http://cabinapi.app 
BROADCAST_DRIVER=redis 
REDIS_HOST=127.0.0.1 
REDIS_PASSWORD=null 
REDIS_PORT=6379 

MessageEvent.php

<?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 MessageEvent implements ShouldBroadcast 
{ 
    use Dispatchable, InteractsWithSockets, SerializesModels; 

    public $count; 
    public function __construct($count) 
    { 
     $this->count = $count; 
    } 

    public function broadcastOn() 
    { 
     return ['message-channel']; 
    } 
} 

則須─>所有者 - > layouts->應用程序。 blade.php

<body> 
<span class="label label-success msgSpan"><span class="msgCountRemove">{!! $miscellaneous->privateMessageCount() !!}</span></span> 

<script src="{{ asset('plugins/jQuery/jquery-2.2.3.min.js') }}"></script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.8/socket.io.min.js"></script> 
    <script> 
     var socket = io('{{ env("APP_URL") }}:3000'); 
     socket.on('message-channel:App\\Events\\MessageEvent', function(data){ 
      $('.msgCountRemove').remove(); 
      $('.msgSpan').append('<span class="msgCountRemove">'+data.count+'</span>'); 
     }); 
    </script> 

</body> 

config-> app.php

'Redis' => Illuminate\Support\Facades\Redis::class, 

socket.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(); 
redis.subscribe('message-channel', function() { 
    console.log('Redis: message-channel subscribed'); 
}); 
redis.on('message', function(channel, message) { 
    console.log('Redis: Message on ' + channel + ' received!'); 
    console.log(message); 
    message = JSON.parse(message); 
    io.emit(channel + ':' + message.event, message.data); 
}); 

io.on('connection', function(socket){ 
    console.log('a user connected'); 
    socket.on('disconnect', function(){ 
     console.log('user disconnected'); 
    }); 
}); 
http.listen(3000, function(){ 
    console.log('listening on *:3000'); 
}); 
+0

你從哪裏得到該日誌?你可以檢查config/broadcasting.php嗎?也許你設置廣播驅動程序登錄那麼.env不工作? –

+0

@HanlinWang,Log我從storage-> logs-> laravel.log。不,我沒有設置廣播日誌。 ''default'=> env('BROADCAST_DRIVER','null'),' –

+0

我在本地測試了你的代碼,它全部正常工作,而且套接字是空的,這是從你的socket.js'{「事件「:」App \\ Events \\ MessageEvent「,」data「:{」count「:1,」socket「:null},」socket「:null} '所以你沒有收到像這樣的日誌,對吧? –

回答

0

找不到爲什麼它不laravel 5.4工作的確切原因。 但我添加了一些更改。現在我正在獲取實時消息通知計數。以下列出了我所做的改變。

我完全做實時通知計數與redis的幫助和socketio

socket.js(Removed ioredis and included redis

var redis = require('redis'); 
io.on('connection', function(socket){ 
    console.log('a user connected'); 

    var redisClient = redis.createClient(); 
    redisClient.subscribe('message'); 
    redisClient.on('message', function(channel, message){ 
     console.log('new message in queue', channel, message); 
     socket.emit(channel, message); 
    }); 

    socket.on('disconnect', function(){ 
     redisClient.quit(); 
     console.log('user disconnected'); 
    }); 
}); 

控制器

$redis = Redis::connection(); 
$redis->publish('message', $message); 

則須─>所有者 - > layouts-> app.blade.php

<script> 
     var socket = io('{{ env("APP_URL") }}:3000'); 
     socket.on('message-channel:App\\Events\\MessageEvent', function(data){ 
      $('.msgCountRemove').remove(); 
      $('.msgSpan').append('<span class="msgCountRemove">'+data+'</span>'); 
     }); 
    </script> 

移除MessageEvent事件。php