Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request websocketconnections :PluginKeyword #6

Closed
hypericum opened this issue Jul 15, 2019 · 4 comments
Closed

feature request websocketconnections :PluginKeyword #6

hypericum opened this issue Jul 15, 2019 · 4 comments

Comments

@hypericum
Copy link

Hi Yanik,

thank you very much for your great Dancer2::Plugin::WebSocket!

I have one little question that I haven't found an answer yet:

How do I send a new WebSocket Message from a Dancer Server through one of the open Sockets to a Client? (not in direct-reply to an incoming message from the client e.g. on_message or something similar)

The documented part (on_message,...) works well, connections are held open and can be found inside the Dancer2::Plugin::WebSocket object.

I'm missing something like:

has websocketconnections :PluginKeyword => (
    is => 'ro',
    default => sub{ {} },
);

instead of just:

has connections => (
    is => 'ro',
    default => sub{ {} },
);

or at least:

Dancer2::Plugin::WebSocket->connections->{$MyClientWebSocketConnectionID}->send($Message);

The shortest way I've found to send a message through an active websocket connection outside the main .pm was:

my $WebSocketConnection = (grep { (ref $_ ) eq 'Dancer2::Plugin::WebSocket' }  @{ app->plugins })[0];

$WebSocketConnection->connections->{ $MyClientWebSocketConnectionID }->send($Message);

If you know a better way please enlight my humble mind :)

Thank you,
Johannes

@yanick
Copy link
Owner

yanick commented Jul 16, 2019

On 2019-07-15 01:39 PM, hypericum wrote:

thank you very much for your great Dancer2::Plugin::WebSocket!

You're very welcome. ^.^

I have one little question that I haven't found an answer yet:

How do I send a new WebSocket Message from a Dancer Server through one of the open Sockets to a Client? (not in direct-reply to an incoming message from the client e.g. on_message or something similar)

You are right. I should make this easier via a keyword. In the meantime, you could do

package MyApp;

use Dancer2;
use Dancer2::Plugin::WebSocket;

my @connections;

websocket_on_open sub {
  my( $conn ) = @_;
  push @connections, $conn;
};

get '/' => sub {
  my $ws_url = websocket_url;
  return $ws_url;
};

get '/greet' => sub {
    # TODO take care of closed connections
    $_->send( "Hi there!" ) for @connections;
    return "done";
};

1;

I'll try to add the new keyword later on today. Hang tight! :-)

@hypericum
Copy link
Author

hypericum commented Jul 16, 2019

this works well as long as I'm in the same scope of MyApp...

If I'm correct in

package MyApp::Foo::Bar:FooBar

@connections wouldn't be available any more.

I've solved it by putting the $Conn->id into the config like:

config->{websocketconnections}->{ $ClientName } = {
    ID         => $Conn->id,
};

in MyApp::Foo::Bar:FooBar sending a message works like that:

my $WebSocketConnection = (grep { (ref $_ ) eq 'Dancer2::Plugin::WebSocket' }  @{ app->plugins })[0];


$WebSocketConnection->connections->{ 
    config->{websocketconnections}->{ $ClientName }->{ID} 
}->send($Message);

...so the main point would be to access @connections from Dancer Packages deeper down in the application, a

use Dancer2::Plugin::WebSocket;

wouldn't be the problem in MyApp::Foo::Bar:FooBar, important would be that the same module-variable @connections would be presented. ;)

THX a lot for your perspective "I'll try to add the new keyword later " ^^

@yanick
Copy link
Owner

yanick commented Jul 16, 2019

If I'm correct in

package MyApp::Foo::Bar:FooBar
@connections wouldn't be available any more.

Yes, true. But if you want to access it from elsewhere, switch the my for a our and you'll be able to access it anywhere at all via @MyApp::connections.

@yanick yanick closed this as completed in bc0adfc Jul 16, 2019
@yanick
Copy link
Owner

yanick commented Jul 16, 2019

This being said, the new keyword websocket_connections is available in the release that I just punted to cpan. :-)

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

No branches or pull requests

2 participants