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');
});
你從哪裏得到該日誌?你可以檢查config/broadcasting.php嗎?也許你設置廣播驅動程序登錄那麼.env不工作? –
@HanlinWang,Log我從storage-> logs-> laravel.log。不,我沒有設置廣播日誌。 ''default'=> env('BROADCAST_DRIVER','null'),' –
我在本地測試了你的代碼,它全部正常工作,而且套接字是空的,這是從你的socket.js'{「事件「:」App \\ Events \\ MessageEvent「,」data「:{」count「:1,」socket「:null},」socket「:null} '所以你沒有收到像這樣的日誌,對吧? –