是否可能調用laravel事件並在socket.io中發送變量?在socket.io中調用laravel事件
這是我想知道是否有可能在插座實現發送插座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以更新數據庫中的記錄的最佳方式。
感謝
我只需要知道什麼時候用戶離開該網站,拿他們的套接字ID和更新數據庫中的記錄,是非常簡單的,但我不知道該怎麼做。 :'C,我試圖用socket.js文件中的對象請求,但不起作用。 –
如果你檢查了這些例子,那麼你會意識到你想要做的就是在他們身上,無論如何,下面我會用XD片段發佈另一個答案! – bretanac93
感謝您的時間,我找到了一個節點請求庫的解決方案,通過服務器上的socker io文件中的socket_id向特定的url發送請求。問候 –