【发布时间】:2017-04-29 00:01:12
【问题描述】:
是否可以调用 laravel 事件并在 socket.io 中发送变量?
这是我想知道是否可以在发送套接字 id 的套接字中实现的行:
socket.emit('App\\Events\\ClosePlaySession', socket.id);
Socket.js
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server, {'pingInterval': 2000, 'pingTimeout': 5000});
var Redis = require('ioredis');
var redis = new Redis();
io.set('heartbeat timeout', 10);
io.set('heartbeat interval', 4);
server.listen(3000);
io.on('connection', function (socket) {
console.log("new client connected " + socket.id);
socket.on('disconnect', function() {
socket.emit('App\\Events\\ClosePlaySession', socket.id); // This is possible?
console.log('client disconnected');
});
});
事件 ClosePlaySession.php
<?php
namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
// Repositories
use wtv\Repositories\PlaySessionRepository;
class ClosePlaySession extends Event implements ShouldBroadcast
{
use SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($socket_id)
{
$playSessionRepository = new PlaySessionRepository;
$playSessionRepository->closeSession($socket_id);
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return ['closePlaySession'];
}
}
或者发送套接字 id 以更新数据库中的记录的最佳方式是什么。
谢谢
【问题讨论】:
标签: php node.js laravel sockets socket.io