« lib/auto/share/dist/App-SocialCalc-Multiplayer/third-party/lib/PocketIO/Transport/Htmlfile.pm
↑ Summary
lib/auto/share/dist/App-SocialCalc-Multiplayer/third-party/lib/PocketIO/Resource.pm »
| 1 | package PocketIO::Transport::XHRMultipart; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'PocketIO::Transport::Base'; | ||
| 7 | |||
| 8 | use PocketIO::Handle; | ||
| 9 | use PocketIO::Response::Chunked; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my $self = shift->SUPER::new(@_); | ||
| 13 | |||
| 14 | $self->{boundary} ||= 'socketio'; | ||
| 15 | |||
| 16 | return $self; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub name {'xhr-multipart'} | ||
| 20 | |||
| 21 | sub dispatch { | ||
| 22 | my $self = shift; | ||
| 23 | my ($cb) = @_; | ||
| 24 | |||
| 25 | my $req = $self->req; | ||
| 26 | my $name = $self->name; | ||
| 27 | |||
| 28 | return $self->_dispatch_stream($req, $cb) if $req->method eq 'GET'; | ||
| 29 | |||
| 30 | return | ||
| 31 | unless $req->method eq 'POST' && $req->path =~ m{^/$name/(\d+)/send$}; | ||
| 32 | |||
| 33 | return $self->_dispatch_send($req, $1); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub _dispatch_stream { | ||
| 37 | my $self = shift; | ||
| 38 | my ($req, $cb) = @_; | ||
| 39 | |||
| 40 | my $handle = $self->_build_handle($req->env->{'psgix.io'}); | ||
| 41 | return unless $handle; | ||
| 42 | |||
| 43 | return sub { | ||
| 44 | my $respond = shift; | ||
| 45 | |||
| 46 | my $conn = $self->add_connection(on_connect => $cb); | ||
| 47 | |||
| 48 | my $close_cb = sub { $handle->close; $self->client_disconnected($conn); }; | ||
| 49 | $handle->on_eof($close_cb); | ||
| 50 | $handle->on_error($close_cb); | ||
| 51 | |||
| 52 | my $boundary = $self->{boundary}; | ||
| 53 | |||
| 54 | $conn->on_write( | ||
| 55 | sub { | ||
| 56 | my $self = shift; | ||
| 57 | my ($message) = @_; | ||
| 58 | |||
| 59 | my $string = ''; | ||
| 60 | |||
| 61 | $string .= "Content-Type: text/plain\x0a\x0a"; | ||
| 62 | if ($message eq '') { | ||
| 63 | $string .= "-1--$boundary--\x0a"; | ||
| 64 | } | ||
| 65 | else { | ||
| 66 | $string .= "$message\x0a--$boundary\x0a"; | ||
| 67 | } | ||
| 68 | |||
| 69 | $handle->write($string); | ||
| 70 | } | ||
| 71 | ); | ||
| 72 | |||
| 73 | $handle->on_heartbeat(sub { $conn->send_heartbeat }); | ||
| 74 | |||
| 75 | $handle->write( | ||
| 76 | join "\x0d\x0a" => 'HTTP/1.1 200 OK', | ||
| 77 | qq{Content-Type: multipart/x-mixed-replace;boundary="$boundary"}, | ||
| 78 | 'Connection: keep-alive', '', '' | ||
| 79 | ); | ||
| 80 | |||
| 81 | $conn->send_id_message($conn->id); | ||
| 82 | |||
| 83 | $self->client_connected($conn); | ||
| 84 | }; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _dispatch_send { | ||
| 88 | my $self = shift; | ||
| 89 | my ($req, $id) = @_; | ||
| 90 | |||
| 91 | my $conn = $self->find_connection($id); | ||
| 92 | return unless $conn; | ||
| 93 | |||
| 94 | my $data = $req->body_parameters->get('data'); | ||
| 95 | |||
| 96 | $conn->read($data); | ||
| 97 | |||
| 98 | return PocketIO::Response::Chunked->finalize; | ||
| 99 | } | ||
| 100 | |||
| 101 | 1; | ||
| 102 | __END__ |