Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Fix/Feature : Fixing ability to add custom handlers to a route by add… #150

Merged
merged 1 commit into from
May 11, 2019
Merged

Conversation

lukepolo
Copy link
Contributor

Fixes #21

Fixing ability to add custom handlers to a route by adding a custom routes method to the router.

This is because the route is registered before the command is fired. The command configures the bindings for the logger, hence why you get an error.

I did not see where we could add this to the docs but this is how you would use it.

<?php

namespace App\Providers;

use App\Services\AssetService;
use App\Services\GuestService;
use Illuminate\Support\ServiceProvider;
use App\WebSocketHandlers\ClientSocketHandler;
use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(AssetService::class, AssetService::class);
        $this->app->bind(GuestService::class, GuestService::class);

        if ($this->app->environment() !== 'production') {
            $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
        }
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        WebSocketsRouter::webSocket('/app/{appKey}/{apiKey}', ClientSocketHandler::class);
    }
}

…ing a custom routes method to the router

Fixing Coding Standards

Fixing Coding Standards
@mpociot mpociot merged commit 556f433 into beyondcode:master May 11, 2019
@mpociot
Copy link
Member

mpociot commented May 11, 2019

Thank you!

@matheusb-comp
Copy link

When the custom routes are registered and the WebsocketsLogger singleton is enabled, the decorate function is applied on all routes, even the custom ones.

However, the WebsocketsLogger should not be applied to custom routes, since it expects variables only available when using the Pusher protocol, as pointed out on #247.

Also, since the Logger is a singleton, the decorate function in the custom route overrides the $app property in the WebsocketLogger, causing any connection to the default /app/{appKey} route to be handled by the custom route, as pointed out on #309.

Preventing the decorate function to be executed on custom routes seems to solve the problems.

protected function createWebSocketsServer(string $action): WsServer
{
    $app = app($action);

    // Do not decorate custom routes or when the Logger is disabled
    if (
        array_search($action, $this->customRoutes->all()) === false &&
        WebsocketsLogger::isEnabled()
    ) {
        $app = WebsocketsLogger::decorate($app);
    }

    return new WsServer($app);
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to register custom WebSocket Handler
3 participants