Se corrigen bugs de chat en tiempo real
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
|
||||
class UserStoppedTyping implements ShouldBroadcast
|
||||
{
|
||||
public $conversationId;
|
||||
public $user;
|
||||
|
||||
public function __construct($conversationId, $user)
|
||||
{
|
||||
$this->conversationId = $conversationId;
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
public function broadcastOn()
|
||||
{
|
||||
return new PrivateChannel('chat.' . $this->conversationId);
|
||||
}
|
||||
|
||||
public function broadcastAs()
|
||||
{
|
||||
return 'UserStoppedTyping';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
|
||||
class UserTyping implements ShouldBroadcast
|
||||
{
|
||||
public $conversationId;
|
||||
public $user;
|
||||
|
||||
public function __construct($conversationId, $user)
|
||||
{
|
||||
$this->conversationId = $conversationId;
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
public function broadcastOn()
|
||||
{
|
||||
return new PrivateChannel('chat.' . $this->conversationId);
|
||||
}
|
||||
|
||||
public function broadcastAs()
|
||||
{
|
||||
return 'UserTyping';
|
||||
}
|
||||
}
|
||||
@@ -13,17 +13,26 @@ class ChatController extends Controller
|
||||
|
||||
public function send(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'conversation_id' => 'required|exists:conversations,id',
|
||||
'message' => 'required|string'
|
||||
]);
|
||||
|
||||
$message = Message::create([
|
||||
'conversation_id' => $request->conversation_id,
|
||||
'user_id' => auth()->id(),
|
||||
'message' => $request->message
|
||||
]);
|
||||
|
||||
// cargar usuario para que venga en el JSON
|
||||
$message->load('user');
|
||||
|
||||
// enviar a los demás usuarios por websocket
|
||||
broadcast(new ChatMessageSent($message))->toOthers();
|
||||
|
||||
return response()->json($message);
|
||||
return response()->json([
|
||||
'message' => $message
|
||||
]);
|
||||
}
|
||||
|
||||
public function start(User $user)
|
||||
@@ -53,14 +62,14 @@ class ChatController extends Controller
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$conversation = Conversation::with([
|
||||
'messages' => function ($query) {
|
||||
$query->orderBy('created_at', 'asc');
|
||||
},
|
||||
'messages.user'
|
||||
])->findOrFail($id);
|
||||
{
|
||||
$conversation = Conversation::with([
|
||||
'messages' => function ($query) {
|
||||
$query->orderBy('created_at', 'asc');
|
||||
},
|
||||
'messages.user'
|
||||
])->findOrFail($id);
|
||||
|
||||
return view('chat.show', compact('conversation'));
|
||||
}
|
||||
return view('chat.show', compact('conversation'));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user