1 | package Mojo::IOLoop::Stream::WebSocketClient;
|
---|
2 | use Mojo::Base 'Mojo::IOLoop::Stream::HTTPClient';
|
---|
3 |
|
---|
4 | use Scalar::Util 'weaken';
|
---|
5 |
|
---|
6 | sub process {
|
---|
7 | my ($self, $tx) = @_;
|
---|
8 | $self->{tx} = $tx;
|
---|
9 | weaken $self;
|
---|
10 | $tx->on(resume => sub { $self->_write_content });
|
---|
11 | $self->_write_content;
|
---|
12 | }
|
---|
13 |
|
---|
14 | sub _finish {
|
---|
15 | my $self = shift;
|
---|
16 | return ++$self->{closing} && $self->close unless $self->{tx};
|
---|
17 | delete($self->{tx})->closed;
|
---|
18 | ++$self->{closing} && $self->close_gracefully;
|
---|
19 | }
|
---|
20 |
|
---|
21 | 1;
|
---|
22 |
|
---|
23 | =encoding utf8
|
---|
24 |
|
---|
25 | =head1 NAME
|
---|
26 |
|
---|
27 | Mojo::IOLoop::Stream::WebSocketClient - Non-blocking I/O WebSocket client stream
|
---|
28 |
|
---|
29 | =head1 SYNOPSIS
|
---|
30 |
|
---|
31 | use Mojo::IOLoop::Stream::WebSocketClient;
|
---|
32 | use Mojo::Transaction::WebSocket;
|
---|
33 |
|
---|
34 | # Create transaction
|
---|
35 | my $ws = Mojo::Transaction::WebSocket->new;
|
---|
36 | $ws->on(message => sub {
|
---|
37 | my ($ws, $msg) = @_;
|
---|
38 | say "Message: $msg";
|
---|
39 | });
|
---|
40 |
|
---|
41 | # Create stream and process transaction with it
|
---|
42 | my $stream = Mojo::IOLoop::Stream::WebSocketClient->new($handle);
|
---|
43 | $stream->process($ws);
|
---|
44 |
|
---|
45 | # Start reactor if necessary
|
---|
46 | $stream->reactor->start unless $stream->reactor->is_running;
|
---|
47 |
|
---|
48 | =head1 DESCRIPTION
|
---|
49 |
|
---|
50 | L<Mojo::IOLoop::Stream::WebSocketClient> is a container for I/O streams used by
|
---|
51 | L<Mojo::IOLoop> to support the WebSocket protocol client-side.
|
---|
52 |
|
---|
53 | =head1 EVENTS
|
---|
54 |
|
---|
55 | L<Mojo::IOLoop::Stream::WebSocketClient> inherits all events from
|
---|
56 | L<Mojo::IOLoop::Stream::HTTPClient>.
|
---|
57 |
|
---|
58 | =head1 ATTRIBUTES
|
---|
59 |
|
---|
60 | L<Mojo::IOLoop::Stream::WebSocketClient> inherits all attributes from
|
---|
61 | L<Mojo::IOLoop::Stream::HTTPClient>.
|
---|
62 |
|
---|
63 | =head1 METHODS
|
---|
64 |
|
---|
65 | L<Mojo::IOLoop::Stream::WebSocketClient> inherits all methods from
|
---|
66 | L<Mojo::IOLoop::Stream::HTTPClient> and implements the following new ones.
|
---|
67 |
|
---|
68 | =head2 process
|
---|
69 |
|
---|
70 | $stream->process(Mojo::Transaction::WebSocket->new);
|
---|
71 |
|
---|
72 | Process a L<Mojo::Transaction::WebSocket> object.
|
---|
73 |
|
---|
74 | =head1 SEE ALSO
|
---|
75 |
|
---|
76 | L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
|
---|
77 |
|
---|
78 | =cut
|
---|
79 |
|
---|