App-SocialCalc-Multiplayer
view release on metacpan or search on metacpan
socialcalc/third-party/lib/PocketIO/Transport/XHRMultipart.pm view on Meta::CPAN
package PocketIO::Transport::XHRMultipart;
use strict;
use warnings;
use base 'PocketIO::Transport::Base';
use PocketIO::Handle;
use PocketIO::Response::Chunked;
sub new {
my $self = shift->SUPER::new(@_);
$self->{boundary} ||= 'socketio';
return $self;
}
sub name {'xhr-multipart'}
sub dispatch {
my $self = shift;
my ($cb) = @_;
my $req = $self->req;
my $name = $self->name;
return $self->_dispatch_stream($req, $cb) if $req->method eq 'GET';
return
unless $req->method eq 'POST' && $req->path =~ m{^/$name/(\d+)/send$};
return $self->_dispatch_send($req, $1);
}
sub _dispatch_stream {
my $self = shift;
my ($req, $cb) = @_;
my $handle = $self->_build_handle($req->env->{'psgix.io'});
return unless $handle;
return sub {
my $respond = shift;
my $conn = $self->add_connection(on_connect => $cb);
my $close_cb = sub { $handle->close; $self->client_disconnected($conn); };
$handle->on_eof($close_cb);
$handle->on_error($close_cb);
my $boundary = $self->{boundary};
$conn->on_write(
sub {
my $self = shift;
my ($message) = @_;
my $string = '';
$string .= "Content-Type: text/plain\x0a\x0a";
if ($message eq '') {
$string .= "-1--$boundary--\x0a";
}
else {
$string .= "$message\x0a--$boundary\x0a";
}
$handle->write($string);
}
);
( run in 1.747 second using v1.01-cache-2.11-cpan-d7f47b0818f )