This commit is contained in:
Alex Renoki 2020-09-15 23:46:37 +03:00
parent 630efa2562
commit 0103e0f9e4
3 changed files with 34 additions and 40 deletions

View File

@ -252,7 +252,7 @@
form: {
channel: null,
event: null,
data: null,
data: {},
},
logs: [],
},
@ -396,6 +396,8 @@
let payload = {
_token: '{{ csrf_token() }}',
appId: this.app.id,
key: this.app.key,
secret: this.app.secret,
channel: this.form.channel,
event: this.form.event,
data: JSON.stringify(this.form.data),

View File

@ -2,52 +2,51 @@
namespace BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers;
use BeyondCode\LaravelWebSockets\Contracts\ChannelManager;
use BeyondCode\LaravelWebSockets\Concerns\PushesToPusher;
use BeyondCode\LaravelWebSockets\Rules\AppId;
use Exception;
use Illuminate\Http\Request;
class SendMessage
{
use PushesToPusher;
/**
* Send the message to the requested channel.
*
* @param \Illuminate\Http\Request $request
* @param \BeyondCode\LaravelWebSockets\Contracts\ChannelManager $channelManager
* @return \Illuminate\Http\Response
*/
public function __invoke(Request $request, ChannelManager $channelManager)
public function __invoke(Request $request)
{
$request->validate([
'appId' => ['required', new AppId],
'key' => 'required|string',
'secret' => 'required|string',
'channel' => 'required|string',
'event' => 'required|string',
'data' => 'required|json',
]);
$payload = [
'channel' => $request->channel,
'event' => $request->event,
'data' => json_decode($request->data, true),
];
$broadcaster = $this->getPusherBroadcaster([
'key' => $request->key,
'secret' => $request->secret,
'id' => $request->appId,
]);
// Here you can use the ->find(), even if the channel
// does not exist on the server. If it does not exist,
// then the message simply will get broadcasted
// across the other servers.
$channel = $channelManager->find(
$request->appId, $request->channel
);
try {
$decodedData = json_decode($request->data, true);
if ($channel) {
$channel->broadcastToEveryoneExcept(
(object) $payload,
null,
$request->appId
);
} else {
$channelManager->broadcastAcrossServers(
$request->appId, $request->channel, (object) $payload
$broadcaster->broadcast(
[$request->channel],
$request->event,
$decodedData ?: []
);
} catch (Exception $e) {
return response()->json([
'ok' => false,
'exception' => $e->getMessage(),
]);
}
return response()->json([

View File

@ -12,28 +12,19 @@ class SendMessageTest extends TestCase
$this->actingAs(factory(User::class)->create())
->json('POST', route('laravel-websockets.event'), [
'appId' => '1234',
'key' => 'TestKey',
'secret' => 'TestSecret',
'channel' => 'test-channel',
'event' => 'some-event',
'data' => json_encode(['data' => 'yes']),
])
->seeJson([
'ok' => true,
'ok' => false,
]);
if (method_exists($this->channelManager, 'getPublishClient')) {
$this->channelManager
->getPublishClient()
->assertCalledWithArgs('publish', [
$this->channelManager->getRedisKey('1234', 'test-channel'),
json_encode([
'channel' => 'test-channel',
'event' => 'some-event',
'data' => ['data' => 'yes'],
'appId' => '1234',
'serverId' => $this->channelManager->getServerId(),
]),
]);
}
$this->markTestIncomplete(
'Broadcasting is not possible to be tested without receiving a Pusher error.'
);
}
public function test_cant_send_message_for_invalid_app()
@ -41,6 +32,8 @@ class SendMessageTest extends TestCase
$this->actingAs(factory(User::class)->create())
->json('POST', route('laravel-websockets.event'), [
'appId' => '9999',
'key' => 'TestKey',
'secret' => 'TestSecret',
'channel' => 'test-channel',
'event' => 'some-event',
'data' => json_encode(['data' => 'yes']),