« lib/auto/share/dist/App-SocialCalc-Multiplayer/third-party/lib/PocketIO/Connection.pm
↑ Summary
lib/auto/share/dist/App-SocialCalc-Multiplayer/third-party/lib/PocketIO/Transport/WebSocket.pm »
| 1 | package PocketIO::Transport::BasePolling; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'PocketIO::Transport::Base'; | ||
| 7 | |||
| 8 | use PocketIO::Response::Chunked; | ||
| 9 | |||
| 10 | sub _dispatch_init { | ||
| 11 | my $self = shift; | ||
| 12 | my ($cb) = @_; | ||
| 13 | |||
| 14 | my $conn; | ||
| 15 | $conn = $self->add_connection( | ||
| 16 | on_connect => $cb, | ||
| 17 | on_reconnect_failed => sub { | ||
| 18 | $self->client_disconnected($conn); | ||
| 19 | } | ||
| 20 | ); | ||
| 21 | |||
| 22 | my $body = $self->_format_message($conn->build_id_message); | ||
| 23 | |||
| 24 | return [ | ||
| 25 | 200, | ||
| 26 | [ 'Content-Type' => 'text/plain', | ||
| 27 | 'Content-Length' => length($body), | ||
| 28 | 'Connection' => 'keep-alive' | ||
| 29 | ], | ||
| 30 | [$body] | ||
| 31 | ]; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub _dispatch_stream { | ||
| 35 | my $self = shift; | ||
| 36 | my ($id) = @_; | ||
| 37 | |||
| 38 | my $conn = $self->find_connection($id); | ||
| 39 | return unless $conn; | ||
| 40 | |||
| 41 | my $handle = $self->_build_handle($self->env->{'psgix.io'}); | ||
| 42 | |||
| 43 | return sub { | ||
| 44 | my $respond = shift; | ||
| 45 | |||
| 46 | my $close_cb = sub { $handle->close; $self->client_disconnected($conn); }; | ||
| 47 | $handle->on_eof($close_cb); | ||
| 48 | $handle->on_error($close_cb); | ||
| 49 | |||
| 50 | $handle->on_heartbeat(sub { $conn->send_heartbeat }); | ||
| 51 | |||
| 52 | if ($conn->has_staged_messages) { | ||
| 53 | $self->_write($conn, $handle, $conn->staged_message); | ||
| 54 | } | ||
| 55 | else { | ||
| 56 | $conn->on_write( | ||
| 57 | sub { | ||
| 58 | my $conn = shift; | ||
| 59 | my ($message) = @_; | ||
| 60 | |||
| 61 | $conn->on_write(undef); | ||
| 62 | $self->_write($conn, $handle, $message); | ||
| 63 | } | ||
| 64 | ); | ||
| 65 | } | ||
| 66 | |||
| 67 | if ($conn->is_connected) { | ||
| 68 | $conn->reconnected; | ||
| 69 | } | ||
| 70 | else { | ||
| 71 | $self->client_connected($conn); | ||
| 72 | } | ||
| 73 | }; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub _dispatch_send { | ||
| 77 | my $self = shift; | ||
| 78 | my ($req, $id) = @_; | ||
| 79 | |||
| 80 | my $conn = $self->find_connection($id); | ||
| 81 | return unless $conn; | ||
| 82 | |||
| 83 | my $data = $req->body_parameters->get('data'); | ||
| 84 | |||
| 85 | $conn->read($data); | ||
| 86 | |||
| 87 | return PocketIO::Response::Chunked->finalize; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub _write { | ||
| 91 | my $self = shift; | ||
| 92 | my ($conn, $handle, $message) = @_; | ||
| 93 | |||
| 94 | $message = $self->_format_message($message); | ||
| 95 | |||
| 96 | $handle->write( | ||
| 97 | join( | ||
| 98 | "\x0d\x0a" => 'HTTP/1.1 200 OK', | ||
| 99 | 'Content-Type: text/plain', | ||
| 100 | 'Content-Length: ' . length($message), '', $message | ||
| 101 | ), | ||
| 102 | sub { | ||
| 103 | $handle->close; | ||
| 104 | $conn->reconnecting; | ||
| 105 | } | ||
| 106 | ); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub _format_message { $_[1] } | ||
| 110 | |||
| 111 | 1; | ||
| 112 | __END__ |