我试图在用户登录网站时显示实时消息通知计数。为此,我正在使用socketio,redis和laravel事件。但它没有用。在laravel日志中,我收到一条消息。在此消息中显示count
,但socket
为空。我想我错过了什么。我的gitbash显示订阅频道和用户已连接。
Gitbash
vagrant@homestead:~/Code/cabinapi$ node socket.js
listening on *:3000
Redis: message-channel subscribed
a user connected
Laravel log
[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\DashboardController@privateMessageAPICount');
.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'];
}
}
&则须─GT;所有者 - &GT; layouts-&GT; app.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-&GT; 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 :(得分: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);
<强>&则须─GT;所有者 - &GT; layouts-&GT; 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