| 1 | package Amon2::Web::WebSocket; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use utf8; | ||
| 5 | |||
| 6 | sub new { | ||
| 7 | my $class = shift; | ||
| 8 | my %args = @_==1 ? %{$_[0]} : @_; | ||
| 9 | bless { | ||
| 10 | %args | ||
| 11 | }, $class; | ||
| 12 | } | ||
| 13 | |||
| 14 | sub on_receive_message { | ||
| 15 | my ( $self, $code ) = @_; | ||
| 16 | $self->{on_receive_message} = $code; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub on_error { | ||
| 20 | my ( $self, $code ) = @_; | ||
| 21 | $self->{on_error} = $code; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub on_eof { | ||
| 25 | my ( $self, $code ) = @_; | ||
| 26 | $self->{on_eof} = $code; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub call_receive_message { | ||
| 30 | my ( $self, $c, $message ) = @_; | ||
| 31 | if ( $self->{on_receive_message} ) { | ||
| 32 | $self->{on_receive_message}->( $c, $message ); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | sub call_error { | ||
| 37 | my ( $self, $c ) = @_; | ||
| 38 | if ( $self->{on_error} ) { | ||
| 39 | $self->{on_error}->($c); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | sub call_eof { | ||
| 44 | my ( $self, $c ) = @_; | ||
| 45 | if ( $self->{on_eof} ) { | ||
| 46 | $self->{on_eof}->($c); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | sub send_message { | ||
| 51 | my ( $self, $message ) = @_; | ||
| 52 | $self->{send_message}->($message); | ||
| 53 | } | ||
| 54 | |||
| 55 | 1; | ||
| 56 | __END__ |