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

View File

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

View File

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