http://www.perlmonks.org?node_id=1033943

Riales has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am trying to write a basic Websocket server with AnyEvent and Protocol::WebSocket::Handshake::Server. However, when I try to test it, the server does not answer the client's request.

I'm watching the traffic with Wireshark and it's looking like the client sends the request and the server responds with an ACK but the server does not send the response.

Here is the server code:

AnyEvent::Socket::tcp_server(undef, undef, sub { my ($fh, $host, $port) = @_; #die; my $handle = AnyEvent->new(fh => $fh); my $hs = Protocol::WebSocket::Handshake::Server->new(); my $chunk = $handle->{rbuf}; $handle->{rbuf} = undef; if (!$hs->is_done) { $hs->parse($chunk); $handle->push_write($hs->to_string); } }, sub { my ($fh, $host, $port) = @_; $self->port($port); $port_is_assigned->send(); }, );

This code is a (hopefully relevant) excerpt from a Moose class. If seeing the entire class would help - just let me know!

What I've noticed is that if I uncomment the #die; line above, the thread the code is running in does not appear to die...so I think what I'm stuck on is that the accept callback never gets called. But the prepare callback gets called no problem.

Also, here's the wireshark traffic:

No. Time Source Destination Protoc +ol Info 1 0.000000 127.0.0.1 127.0.0.1 TCP + 44315 > 53196 [SYN] Seq=0 Win=32792 Len=0 MSS=16396 TSV=1875375 TS +ER=0 WS=6 Frame 1 (74 bytes on wire, 74 bytes captured) Ethernet II, Src: 00:00:00_00:00:00 (00:00:00:00:00:00), Dst: 00:00:00 +_00:00:00 (00:00:00:00:00:00) Internet Protocol, Src: 127.0.0.1 (127.0.0.1), Dst: 127.0.0.1 (127.0.0 +.1) Transmission Control Protocol, Src Port: 44315 (44315), Dst Port: 5319 +6 (53196), Seq: 0, Len: 0 No. Time Source Destination Protoc +ol Info 2 0.000019 127.0.0.1 127.0.0.1 TCP + 53196 > 44315 [SYN, ACK] Seq=0 Ack=1 Win=32768 Len=0 MSS=16396 TSV +=1875375 TSER=1875375 WS=6 Frame 2 (74 bytes on wire, 74 bytes captured) Ethernet II, Src: 00:00:00_00:00:00 (00:00:00:00:00:00), Dst: 00:00:00 +_00:00:00 (00:00:00:00:00:00) Internet Protocol, Src: 127.0.0.1 (127.0.0.1), Dst: 127.0.0.1 (127.0.0 +.1) Transmission Control Protocol, Src Port: 53196 (53196), Dst Port: 4431 +5 (44315), Seq: 0, Ack: 1, Len: 0 No. Time Source Destination Protoc +ol Info 3 0.000033 127.0.0.1 127.0.0.1 TCP + 44315 > 53196 [ACK] Seq=1 Ack=1 Win=32832 Len=0 TSV=1875375 TSER=1 +875375 Frame 3 (66 bytes on wire, 66 bytes captured) Ethernet II, Src: 00:00:00_00:00:00 (00:00:00:00:00:00), Dst: 00:00:00 +_00:00:00 (00:00:00:00:00:00) Internet Protocol, Src: 127.0.0.1 (127.0.0.1), Dst: 127.0.0.1 (127.0.0 +.1) Transmission Control Protocol, Src Port: 44315 (44315), Dst Port: 5319 +6 (53196), Seq: 1, Ack: 1, Len: 0 No. Time Source Destination Protoc +ol Info 4 0.000553 127.0.0.1 127.0.0.1 TCP + 44315 > 53196 [PSH, ACK] Seq=1 Ack=1 Win=32832 Len=186 TSV=1875375 + TSER=1875375 Frame 4 (252 bytes on wire, 252 bytes captured) Ethernet II, Src: 00:00:00_00:00:00 (00:00:00:00:00:00), Dst: 00:00:00 +_00:00:00 (00:00:00:00:00:00) Internet Protocol, Src: 127.0.0.1 (127.0.0.1), Dst: 127.0.0.1 (127.0.0 +.1) Transmission Control Protocol, Src Port: 44315 (44315), Dst Port: 5319 +6 (53196), Seq: 1, Ack: 1, Len: 186 Data (186 bytes) 0000 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 31 0d 0a GET / HTTP/1.1 +.. 0010 55 70 67 72 61 64 65 3a 20 57 65 62 53 6f 63 6b Upgrade: WebSo +ck 0020 65 74 0d 0a 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 et..Connection +: 0030 55 70 67 72 61 64 65 0d 0a 48 6f 73 74 3a 20 31 Upgrade..Host: + 1 0040 32 37 2e 30 2e 30 2e 31 3a 35 33 31 39 36 0d 0a 27.0.0.1:53196 +.. 0050 4f 72 69 67 69 6e 3a 20 68 74 74 70 3a 2f 2f 31 Origin: http:/ +/1 0060 32 37 2e 30 2e 30 2e 31 3a 35 33 31 39 36 0d 0a 27.0.0.1:53196 +.. 0070 53 65 63 2d 57 65 62 53 6f 63 6b 65 74 2d 4b 65 Sec-WebSocket- +Ke 0080 79 3a 20 33 45 42 4d 47 79 6e 4e 48 43 35 6a 4f y: 3EBMGynNHC5 +jO 0090 39 37 74 30 6e 6c 72 35 67 3d 3d 0d 0a 53 65 63 97t0nlr5g==..S +ec 00a0 2d 57 65 62 53 6f 63 6b 65 74 2d 56 65 72 73 69 -WebSocket-Ver +si 00b0 6f 6e 3a 20 31 33 0d 0a 0d 0a on: 13.... No. Time Source Destination Protoc +ol Info 5 0.000568 127.0.0.1 127.0.0.1 TCP + 53196 > 44315 [ACK] Seq=1 Ack=187 Win=32768 Len=0 TSV=1875375 TSER +=1875375 Frame 5 (66 bytes on wire, 66 bytes captured) Ethernet II, Src: 00:00:00_00:00:00 (00:00:00:00:00:00), Dst: 00:00:00 +_00:00:00 (00:00:00:00:00:00) Internet Protocol, Src: 127.0.0.1 (127.0.0.1), Dst: 127.0.0.1 (127.0.0 +.1) Transmission Control Protocol, Src Port: 53196 (53196), Dst Port: 4431 +5 (44315), Seq: 1, Ack: 187, Len: 0

Replies are listed 'Best First'.
Re: WebSocket Server Not Responding
by Riales (Hermit) on May 28, 2013 at 17:31 UTC
    For the curious, the problem turned out to be scoping issue. $handle would go out of scope once the server initialized. The fix turned out to just be declaring my $handle; before actually creating it in the on-connect callback for tcp_server.