diff --git a/src/Dashboard/Http/Controllers/SendMessage.php b/src/Dashboard/Http/Controllers/SendMessage.php index 54da651..c8d84d8 100644 --- a/src/Dashboard/Http/Controllers/SendMessage.php +++ b/src/Dashboard/Http/Controllers/SendMessage.php @@ -4,6 +4,7 @@ namespace BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers; use BeyondCode\LaravelWebSockets\Contracts\PushesToPusher; use BeyondCode\LaravelWebSockets\Statistics\Rules\AppId; +use Exception; use Illuminate\Http\Request; class SendMessage @@ -33,12 +34,21 @@ class SendMessage 'id' => $request->appId, ]); - $broadcaster->broadcast( - [$request->channel], - $request->event, - json_decode($request->data, true) - ); + try { + $broadcaster->broadcast( + [$request->channel], + $request->event, + json_decode($request->data, true) + ); + } catch (Exception $e) { + return response()->json([ + 'ok' => false, + 'exception' => $e->getMessage(), + ]); + } - return 'ok'; + return response()->json([ + 'ok' => true, + ]); } } diff --git a/tests/Dashboard/SendMessageTest.php b/tests/Dashboard/SendMessageTest.php new file mode 100644 index 0000000..0d466ee --- /dev/null +++ b/tests/Dashboard/SendMessageTest.php @@ -0,0 +1,53 @@ +skipOnRedisReplication(); + + // Because the Pusher server is not active, + // we expect it to turn out ok: false. + + $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' => false, + ]); + } + + /** @test */ + public function cant_send_message_for_invalid_app() + { + $this->skipOnRedisReplication(); + + // Because the Pusher server is not active, + // we expect it to turn out ok: false. + + $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']), + ]) + ->assertResponseStatus(422); + } +}