File: Message.pm

package info (click to toggle)
libanyevent-websocket-client-perl 0.55-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 420 kB
  • sloc: perl: 1,022; sh: 5; makefile: 2
file content (166 lines) | stat: -rw-r--r-- 2,606 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package AnyEvent::WebSocket::Message;

use strict;
use warnings;
use Moo;
use Encode ();

# ABSTRACT: WebSocket message for AnyEvent
our $VERSION = '0.55'; # VERSION


has body => ( is => 'ro', required => 1 );
has opcode => ( is => 'ro', default => 1 );


sub decoded_body
{
  Encode::decode("UTF-8", shift->body)
}


sub is_text   { $_[0]->opcode == 1 }
sub is_binary { $_[0]->opcode == 2 }
sub is_close  { $_[0]->opcode == 8 }
sub is_ping   { $_[0]->opcode == 9 }
sub is_pong   { $_[0]->opcode == 10 }

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

AnyEvent::WebSocket::Message - WebSocket message for AnyEvent

=head1 VERSION

version 0.55

=head1 SYNOPSIS

 $connection->send(
   AnyEvent::WebSocket::Message->new(body => "some message"),
 );
 
 $connection->on(each_message => sub {
   my($connection, $message) = @_;
   if($message->is_text || $message->is_binary)
   {
     my $body = $message->body;
   }
 });

=head1 DESCRIPTION

Instances of this class represent a single WebSocket message.  They are
the objects that come through from the other end of your
L<AnyEvent::WebSocket::Connection> instance.  They can also be sent through
that class using its C<send> method.

=head1 ATTRIBUTES

=head2 body

The body or payload of the message.

=head2 opcode

The integer code for the type of message.

=head1 METHODS

=head2 decoded_body

 my $body = $message->decoded_body;

Returns the body decoded from UTF-8.

=head2 is_text

 my $bool = $message->is_text;

True if the message is text.

=head2 is_binary

 my $bool = $message->is_binary;

True if the message is binary.

=head2 is_close

 my $bool = $message->is_close;

True if the message is a close message.

=head2 is_ping

 my $bool = $message->is_ping

True if the message is a ping.

=head2 is_pong

 my $bool = $message->is_pong;

True if the message is a pong.

=head1 SEE ALSO

=over 4

=item *

L<AnyEvent::WebSocket::Client>

=item *

L<AnyEvent::WebSocket::Connection>

=item *

L<AnyEvent::WebSocket::Server>

=item *

L<AnyEvent>

=item *

L<RFC 6455 The WebSocket Protocol|http://tools.ietf.org/html/rfc6455>

=back

=for stopwords Joaquín José

=head1 AUTHOR

Author: Graham Ollis E<lt>plicease@cpan.orgE<gt>

Contributors:

Toshio Ito (debug-ito, TOSHIOITO)

José Joaquín Atria (JJATRIA)

Kivanc Yazan (KYZN)

Yanick Champoux (YANICK)

Fayland Lam (FAYLAND)

Daniel Kamil Kozar (xavery)

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2013-2022 by Graham Ollis.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut