| 1 | package Net::WebSocket; | ||
| 2 | |||
| 3 | our $VERSION = '0.05-TRIAL7'; | ||
| 4 | |||
| 5 | =encoding utf-8 | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Net::WebSocket - WebSocket in Perl | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use Net::WebSocket::Handshake::Client (); | ||
| 14 | use Net::WebSocket::HTTP_R (); | ||
| 15 | |||
| 16 | my $handshake = Net::WebSocket::Handshake::Client->new( | ||
| 17 | uri => $uri, | ||
| 18 | ); | ||
| 19 | |||
| 20 | syswrite $inet, $handshake->to_string() or die $!; | ||
| 21 | |||
| 22 | #You can parse HTTP headers however you want; | ||
| 23 | #Net::WebSocket makes no assumptions about this. | ||
| 24 | my $resp = HTTP::Response->parse($hdrs_txt); | ||
| 25 | |||
| 26 | #If you use an interface that’s compatible with HTTP::Response, | ||
| 27 | #then you can take advantage of this convenience function; | ||
| 28 | #otherwise you’ll need to do a bit more work. | ||
| 29 | Net::WebSocket::HTTP_R::handshake_parse_response( $handshake, $resp ); | ||
| 30 | |||
| 31 | #See below about IO::Framed | ||
| 32 | my $parser = Net::WebSocket::Parser->new( | ||
| 33 | IO::Framed::Read->new($inet), | ||
| 34 | $leftover_from_header_read, #can be nonempty on the client | ||
| 35 | ); | ||
| 36 | |||
| 37 | my $iof_w = IO::Framed::Write->new($inet); | ||
| 38 | |||
| 39 | my $ept = Net::WebSocket::Endpoint::Client->new( | ||
| 40 | parser => $parser, | ||
| 41 | out => $iof_w, | ||
| 42 | ); | ||
| 43 | |||
| 44 | $iof_w->write( | ||
| 45 | Net::WebSocket::Frame::text->new( payload_sr => \'Hello, world' ) | ||
| 46 | ); | ||
| 47 | |||
| 48 | #Determine that $inet can be read from … | ||
| 49 | |||
| 50 | my $msg = $ept->get_next_message(); | ||
| 51 | |||
| 52 | #… or, if we timeout while waiting for $inet to be ready for reading: | ||
| 53 | |||
| 54 | $ept->check_heartbeat(); | ||
| 55 | exit if $ept->is_closed(); | ||
| 56 | |||
| 57 | =head1 BETA QUALITY | ||
| 58 | |||
| 59 | This is a beta release. It should be safe for production, but there could | ||
| 60 | still be small changes to the API. Please check the changelog before | ||
| 61 | upgrading. | ||
| 62 | |||
| 63 | =head1 DESCRIPTION | ||
| 64 | |||
| 65 | This distribution provides a set of fundamental tools for communicating via | ||
| 66 | L<WebSocket|https://tools.ietf.org/html/rfc6455>. | ||
| 67 | It is only concerned with the protocol itself; | ||
| 68 | the underlying transport mechanism is up to you: it could be a file, | ||
| 69 | a UNIX socket, ordinary TCP/IP, some funky C<tie()>d object, or whatever. | ||
| 70 | |||
| 71 | Net::WebSocket also “has no opinions” about how you should do I/O or HTTP | ||
| 72 | headers. There are too many different ways to accomplish HTTP header | ||
| 73 | management in particular for it to be sensible for a WebSocket library to | ||
| 74 | impose any one approach. As a result of this, Net::WebSocket can probably | ||
| 75 | fit your project with minimal overhead. There are some examples | ||
| 76 | of how you might write complete applications (client or server) | ||
| 77 | in the distribution’s F<demo/> directory. | ||
| 78 | |||
| 79 | Net::WebSocket emphasizes flexibility and lightness rather than the more | ||
| 80 | monolithic approach in modules like L<Mojolicious>. | ||
| 81 | Net::WebSocket should support anything | ||
| 82 | that the WebSocket protocol itself can do, as lightly as possible and without | ||
| 83 | prejudice as to how you want to do it: extensions, blocking/non-blocking I/O, | ||
| 84 | arbitrary HTTP headers, etc. Net::WebSocket will likely require more of an | ||
| 85 | investment up-front, but the end result should be a clean, light | ||
| 86 | implementation that will grow (or shrink!) as your needs dictate. | ||
| 87 | |||
| 88 | =head1 OVERVIEW | ||
| 89 | |||
| 90 | Here are the main modules: | ||
| 91 | |||
| 92 | =over | ||
| 93 | |||
| 94 | =item L<Net::WebSocket::Handshake::Server> | ||
| 95 | |||
| 96 | =item L<Net::WebSocket::Handshake::Client> | ||
| 97 | |||
| 98 | Logic for handshakes. Every application needs one of these. As of version | ||
| 99 | 0.5 this handles all headers and can also do | ||
| 100 | subprotocol and extension negotiation for you. | ||
| 101 | |||
| 102 | =item L<Net::WebSocket::HTTP_R> | ||
| 103 | |||
| 104 | A thin convenience wrapper for L<HTTP::Request> and L<HTTP::Response>, | ||
| 105 | CPAN’s “standard” classes to represent HTTP requests and responses. | ||
| 106 | Net::WebSocket::HTTP_R should also work with other classes whose | ||
| 107 | interfaces are compatible with these “standard” ones. | ||
| 108 | |||
| 109 | =item L<Net::WebSocket::Endpoint::Server> | ||
| 110 | |||
| 111 | =item L<Net::WebSocket::Endpoint::Client> | ||
| 112 | |||
| 113 | A high-level abstraction to parse input | ||
| 114 | and respond to control frames and timeouts. You can use this to receive | ||
| 115 | streamed (i.e., fragmented) transmissions as well. You don’t have to use | ||
| 116 | this module, but it will make your life easier. | ||
| 117 | |||
| 118 | =item L<Net::WebSocket::Parser> | ||
| 119 | |||
| 120 | Translate WebSocket frames out of a filehandle into useful data for | ||
| 121 | your application. | ||
| 122 | |||
| 123 | =item Net::WebSocket::Frame::* | ||
| 124 | |||
| 125 | Useful for creating raw frames. For data frames (besides continuation), | ||
| 126 | these will be your bread-and-butter. See L<Net::WebSocket::Frame::text> | ||
| 127 | for sample usage. | ||
| 128 | |||
| 129 | =item L<Net::WebSocket::Streamer::Server> | ||
| 130 | |||
| 131 | =item L<Net::WebSocket::Streamer::Client> | ||
| 132 | |||
| 133 | Useful for sending streamed (fragmented) data rather than | ||
| 134 | a full message in a single frame. | ||
| 135 | |||
| 136 | =back | ||
| 137 | |||
| 138 | =head1 IMPLEMENTATION NOTES | ||
| 139 | |||
| 140 | =head2 Handshakes | ||
| 141 | |||
| 142 | WebSocket uses regular HTTP headers for its handshakes. Because there are | ||
| 143 | many different solutions around for parsing HTTP headers, Net::WebSocket | ||
| 144 | is “agnostic” about how that’s done. The advantage is that if you’ve got | ||
| 145 | a custom solution for parsing headers then Net::WebSocket can fit into | ||
| 146 | that quite easily. | ||
| 147 | |||
| 148 | The liability of this is that you, the library user, must give headers | ||
| 149 | directly to your Handshake object. (NB: L<Net::WebSocket::HTTP_R> might | ||
| 150 | be able to do this for you.) | ||
| 151 | |||
| 152 | =head2 Masking | ||
| 153 | |||
| 154 | As per L<the specification|https://tools.ietf.org/html/rfc6455#section-5.1>, | ||
| 155 | client serializers “MUST” mask the data randomly, whereas server serializers | ||
| 156 | “MUST NOT” do this. Net::WebSocket does this for you automatically, | ||
| 157 | but you need to distinguish | ||
| 158 | between client serializers—which mask their payloads—and server serializers, | ||
| 159 | which don’t mask. | ||
| 160 | |||
| 161 | This module used to do this with L<Bytes::Random::Secure::Tiny>, but | ||
| 162 | that seems like overkill given that the masking is only there to accommodate | ||
| 163 | peculiarities of certain proxies. We now just use Perl’s C<rand()> | ||
| 164 | built-in. | ||
| 165 | |||
| 166 | (You should probably use TLS if cryptographically secure masking is something | ||
| 167 | you actually care about?) | ||
| 168 | |||
| 169 | =head2 Text vs. Binary | ||
| 170 | |||
| 171 | Recall that in some languages—like JavaScript!—the difference between | ||
| 172 | “text” and “binary” is much more significant than for us in Perl. | ||
| 173 | |||
| 174 | =head2 Use of L<IO::Framed> | ||
| 175 | |||
| 176 | CPAN’s L<IO::Framed> provides a straightforward interface for chunking up | ||
| 177 | data from byte streams into frames. It also provides a write buffer for | ||
| 178 | non-blocking writes, and it retries on EINTR. You don’t have to use it | ||
| 179 | (which is why it’s not listed as a requirement), but you’ll need to provide | ||
| 180 | a compatible interface if you don’t. | ||
| 181 | |||
| 182 | See the demo scripts that use L<IO::Framed> for an example of when you may | ||
| 183 | need a different solution here. | ||
| 184 | |||
| 185 | =head1 EXTENSION SUPPORT | ||
| 186 | |||
| 187 | The WebSocket specification describes several methods of extending the | ||
| 188 | protocol, all of which Net::WebSocket supports: | ||
| 189 | |||
| 190 | =over | ||
| 191 | |||
| 192 | =item * The three reserved bits in each frame’s header. | ||
| 193 | (See L<Net::WebSocket::Frame>.) This is used, e.g., in the | ||
| 194 | L<permessage-deflate extension|https://tools.ietf.org/html/rfc7692>. | ||
| 195 | |||
| 196 | =item * Additional opcodes: 3-7 and 11-15. You’ll need to subclass | ||
| 197 | L<Net::WebSocket::Frame> for this, and you will likely want to subclass | ||
| 198 | L<Net::WebSocket::Parser>. | ||
| 199 | If you’re using the custom classes for streaming, then you can | ||
| 200 | also subclass L<Net::WebSocket::Streamer>. See each of those modules for | ||
| 201 | more information on doing this. | ||
| 202 | |||
| 203 | B<THIS IS NOT WELL TESTED.> Proceed with caution, and please file bug | ||
| 204 | reports as needed. (I personally don’t know of any applications that | ||
| 205 | actually use this.) | ||
| 206 | |||
| 207 | =item * Apportion part of the payload data for the extension. This you | ||
| 208 | can do in your application. | ||
| 209 | |||
| 210 | =back | ||
| 211 | |||
| 212 | =head2 permessage-deflate | ||
| 213 | |||
| 214 | Net::WebSocket 0.5 introduces support for the permessage-delate extension | ||
| 215 | to allow compressed messages over WebSocket. See | ||
| 216 | L<Net::WebSocket::PMCE::deflate> for more details. | ||
| 217 | |||
| 218 | =head1 TODO | ||
| 219 | |||
| 220 | At this point Net::WebSocket should support every widely implemented | ||
| 221 | WebSocket feature. | ||
| 222 | |||
| 223 | =over | ||
| 224 | |||
| 225 | =item * Add more tests. | ||
| 226 | |||
| 227 | =back | ||
| 228 | |||
| 229 | =head1 SEE ALSO | ||
| 230 | |||
| 231 | L<Mojolicious> is probably CPAN’s easiest WebSocket implementation to get | ||
| 232 | a server up and running. If you’re building a project from scratch, you | ||
| 233 | may find this to be a better fit for you than Net::WebSocket. | ||
| 234 | |||
| 235 | L<Protocol::WebSocket> is an older module that supports | ||
| 236 | pre-standard versions of the WebSocket protocol. It’s similar to this one | ||
| 237 | in that it gives you just the protocol itself, but it doesn’t give you | ||
| 238 | things like automatic ping/pong/close, classes for each message type, etc. | ||
| 239 | |||
| 240 | L<Net::WebSocket::Server> implements only server behaviors and | ||
| 241 | gives you more automation than P::WS. | ||
| 242 | |||
| 243 | L<Net::WebSocket::EV> uses XS to call a C library. | ||
| 244 | |||
| 245 | =head1 REPOSITORY | ||
| 246 | |||
| 247 | L<https://github.com/FGasper/p5-Net-WebSocket> | ||
| 248 | |||
| 249 | =head1 AUTHOR | ||
| 250 | |||
| 251 | Felipe Gasper (FELIPE) | ||
| 252 | |||
| 253 | =head1 COPYRIGHT | ||
| 254 | |||
| 255 | Copyright 2017 by L<Gasper Software Consulting, LLC|http://gaspersoftware.com> | ||
| 256 | |||
| 257 | =head1 LICENSE | ||
| 258 | |||
| 259 | This distribution is released under the same license as Perl. | ||
| 260 | |||
| 261 | =cut | ||
| 262 | |||
| 263 | 1; |