« lib/Beekeeper/Service/Supervisor.pm
↑ Summary
blib/lib/auto/share/dist/Beekeeper/chat/lib/MyApp/Service/Auth/Worker.pm »
| 1 | package MyApp::Service::Chat; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Beekeeper::Client; | ||
| 7 | use Time::HiRes 'time'; | ||
| 8 | |||
| 9 | |||
| 10 | sub new { | ||
| 11 | my $class = shift; | ||
| 12 | bless {}, $class; | ||
| 13 | } | ||
| 14 | |||
| 15 | sub client { | ||
| 16 | my $proto = shift; | ||
| 17 | |||
| 18 | Beekeeper::Client->instance; | ||
| 19 | } | ||
| 20 | |||
| 21 | |||
| 22 | # This is the API of service MyApp::Service::Chat | ||
| 23 | |||
| 24 | sub send_message { | ||
| 25 | my ($self, %args) = @_; | ||
| 26 | |||
| 27 | $self->client->call_remote( | ||
| 28 | method => 'myapp.chat.message', | ||
| 29 | params => { | ||
| 30 | message => $args{'message'}, | ||
| 31 | }, | ||
| 32 | ); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub send_private_message { | ||
| 36 | my ($self, %args) = @_; | ||
| 37 | |||
| 38 | $self->client->call_remote( | ||
| 39 | method => 'myapp.chat.pmessage', | ||
| 40 | params => { | ||
| 41 | to_user => $args{'to_user'}, | ||
| 42 | message => $args{'message'}, | ||
| 43 | }, | ||
| 44 | ); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub send_notice { | ||
| 48 | my ($self, %args) = @_; | ||
| 49 | |||
| 50 | $self->client->call_remote( | ||
| 51 | method => 'myapp.chat.notice', | ||
| 52 | params => { | ||
| 53 | to_uuid => $args{'to_uuid'}, | ||
| 54 | message => $args{'message'}, | ||
| 55 | }, | ||
| 56 | ); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub ping { | ||
| 60 | my ($self) = @_; | ||
| 61 | |||
| 62 | my $start = time; | ||
| 63 | |||
| 64 | $self->client->call_remote( method => 'myapp.chat.ping' ); | ||
| 65 | |||
| 66 | my $took = time - $start; | ||
| 67 | |||
| 68 | return sprintf("%.1f", $took * 1000); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub receive_messages { | ||
| 72 | my ($self, %args) = @_; | ||
| 73 | |||
| 74 | my $callback = $args{'callback'}; | ||
| 75 | |||
| 76 | die "Callback must be a coderef" unless (ref $callback eq 'CODE'); | ||
| 77 | |||
| 78 | $self->client->accept_notifications( | ||
| 79 | 'myapp.chat.*' => sub { | ||
| 80 | my $params = shift; | ||
| 81 | $callback->( | ||
| 82 | message => $params->{'message'}, | ||
| 83 | from => $params->{'from'}, | ||
| 84 | ); | ||
| 85 | }, | ||
| 86 | ); | ||
| 87 | } | ||
| 88 | |||
| 89 | 1; |