Hot questions for Using Transmission Control Protocol in websocket

Question:

I'm currently developing a Java WebSocket Client Application and I have to make sure that every message from the server is received by the client. Is it possible that I lose some messages (once they are sent from the server) due to a connection interruption? WebSocket is based on TCP so this shouldn't happen right?


Answer:

It can happen. TCP guarantees the order of packets, but it does not mean that all packets sent from a server reach a client even when an unrecoverable trouble happens in an underlying network. Imagine someone pulls out your LAN cable or switches off your WiFi access point at the worst timing while your application is communicating with your server. TCP does not overcome such a trouble.

To ensure that every WebSocket message sent from your server reaches your client, you have to implement some kind of SYN/ACK in the application layer.

Question:

I am trying to write Java code to receive data from a streaming HTTP connection (I would like to debug a problem I have with running Sockjs XHR-streaming against a spring-websocket server).

I use some really simple code to connect:

    URI uri = // The server
    Socket socket = new Socket();
    socket.connect(new InetSocketAddress(uri.getHost(), uri.getPort()));
    OutputStream os = socket.getOutputStream();
    PrintWriter pw = new PrintWriter(os);
    int id = new Random().nextInt() % 100000;
    pw.println("POST /socket/" + id + "/xxx/xhr_streaming HTTP/1.1");
    pw.println("Content-Type: application/stomp");
    pw.println("Content-Length: 0");
    pw.println("Connection: keep-alive");
    pw.println();
    pw.flush();
    InputStream is = socket.getInputStream();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[8192];
    int bytes;
    do {
        bytes = is.read(buffer);
        if (bytes > 0) {
            System.out.println("read: "+ bytes);
            System.out.print(new String(buffer, 0, bytes));
            System.out.println("----");
        }
    } while (bytes >= 0);

but when I run it I only get the HTTP headers.

That would not be too surprising in itself. I would suspect that the server did not send the contents properly.

But when I monitor the traffic using Wireshark, I can see that I receive three TCP segments in response to my POST. One containing 1460 bytes (+54 bytes TCP/IP headers), one of 1178 bytes (+54 bytes header) and one of 7 bytes (+44 bytes header).

The first one contains the HTTP headers as well as 878 bytes of the contents (Sockjs XHR streaming starts with sending a preamble of 2048 h's). The next one contains the rest of the preamble and the final one contains the 'open frame'. Exactly as I would expect from the server.

Why do my Java code not receive the remaining data? When I use a raw Socket it should be completely unaware of the HTTP protocol, so I do not understand why I receive only the HTTP headers. The server probably flushes the data after writing the headers, but I can see in the network-dump that both the headers and the first part of the data is contained in the first TCP segment.

Here is a dump of the first received TCP segment:

0000   e8 39 35 45 20 df 00 0c 29 09 62 3b 08 00 45 00  .95E ...).b;..E.
0010   05 dc 0c 5e 40 00 80 06 5b 27 c0 a8 06 26 c0 a8  ...^@...['...&..
0020   06 20 1f 90 df d9 92 50 c3 f6 ac 89 8a c3 50 10  . .....P......P.
0030   01 00 fc 88 00 00 48 54 54 50 2f 31 2e 31 20 32  ......HTTP/1.1 2
0040   30 30 20 4f 4b 0d 0a 53 65 72 76 65 72 3a 20 41  00 OK..Server: A
0050   70 61 63 68 65 2d 43 6f 79 6f 74 65 2f 31 2e 31  pache-Coyote/1.1
0060   0d 0a 58 2d 55 41 2d 43 6f 6d 70 61 74 69 62 6c  ..X-UA-Compatibl
0070   65 3a 20 49 45 3d 65 64 67 65 2c 63 68 72 6f 6d  e: IE=edge,chrom
0080   65 3d 31 0d 0a 58 2d 43 6f 6e 74 65 6e 74 2d 54  e=1..X-Content-T
0090   79 70 65 2d 4f 70 74 69 6f 6e 73 3a 20 6e 6f 73  ype-Options: nos
00a0   6e 69 66 66 0d 0a 58 2d 58 53 53 2d 50 72 6f 74  niff..X-XSS-Prot
00b0   65 63 74 69 6f 6e 3a 20 31 3b 20 6d 6f 64 65 3d  ection: 1; mode=
00c0   62 6c 6f 63 6b 0d 0a 43 61 63 68 65 2d 43 6f 6e  block..Cache-Con
00d0   74 72 6f 6c 3a 20 6e 6f 2d 63 61 63 68 65 2c 20  trol: no-cache, 
00e0   6e 6f 2d 73 74 6f 72 65 2c 20 6d 61 78 2d 61 67  no-store, max-ag
00f0   65 3d 30 2c 20 6d 75 73 74 2d 72 65 76 61 6c 69  e=0, must-revali
0100   64 61 74 65 0d 0a 50 72 61 67 6d 61 3a 20 6e 6f  date..Pragma: no
0110   2d 63 61 63 68 65 0d 0a 45 78 70 69 72 65 73 3a  -cache..Expires:
0120   20 30 0d 0a 58 2d 46 72 61 6d 65 2d 4f 70 74 69   0..X-Frame-Opti
0130   6f 6e 73 3a 20 44 45 4e 59 0d 0a 58 2d 46 72 61  ons: DENY..X-Fra
0140   6d 65 2d 4f 70 74 69 6f 6e 73 3a 20 41 4c 4c 4f  me-Options: ALLO
0150   57 2d 46 52 4f 4d 20 2a 0d 0a 58 2d 41 70 70 6c  W-FROM *..X-Appl
0160   69 63 61 74 69 6f 6e 2d 43 6f 6e 74 65 78 74 3a  ication-Context:
0170   20 61 70 70 6c 69 63 61 74 69 6f 6e 0d 0a 43 61   application..Ca
0180   63 68 65 2d 43 6f 6e 74 72 6f 6c 3a 20 6e 6f 2d  che-Control: no-
0190   73 74 6f 72 65 2c 20 6e 6f 2d 63 61 63 68 65 2c  store, no-cache,
01a0   20 6d 75 73 74 2d 72 65 76 61 6c 69 64 61 74 65   must-revalidate
01b0   2c 20 6d 61 78 2d 61 67 65 3d 30 0d 0a 41 63 63  , max-age=0..Acc
01c0   65 73 73 2d 43 6f 6e 74 72 6f 6c 2d 41 6c 6c 6f  ess-Control-Allo
01d0   77 2d 4f 72 69 67 69 6e 3a 20 2a 0d 0a 41 63 63  w-Origin: *..Acc
01e0   65 73 73 2d 43 6f 6e 74 72 6f 6c 2d 41 6c 6c 6f  ess-Control-Allo
01f0   77 2d 43 72 65 64 65 6e 74 69 61 6c 73 3a 20 74  w-Credentials: t
0200   72 75 65 0d 0a 43 6f 6e 74 65 6e 74 2d 54 79 70  rue..Content-Typ
0210   65 3a 20 61 70 70 6c 69 63 61 74 69 6f 6e 2f 6a  e: application/j
0220   61 76 61 73 63 72 69 70 74 3b 63 68 61 72 73 65  avascript;charse
0230   74 3d 55 54 46 2d 38 0d 0a 54 72 61 6e 73 66 65  t=UTF-8..Transfe
0240   72 2d 45 6e 63 6f 64 69 6e 67 3a 20 63 68 75 6e  r-Encoding: chun
0250   6b 65 64 0d 0a 44 61 74 65 3a 20 46 72 69 2c 20  ked..Date: Fri, 
0260   32 36 20 53 65 70 20 32 30 31 34 20 30 39 3a 32  26 Sep 2014 09:2
0270   36 3a 31 37 20 47 4d 54 0d 0a 0d 0a 38 30 31 0d  6:17 GMT....801.
0280   0a 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  .hhhhhhhhhhhhhhh
0290   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
02a0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
02b0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
02c0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
02d0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
02e0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
02f0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0300   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0310   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0320   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0330   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0340   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0350   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0360   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0370   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0380   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0390   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
03a0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
03b0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
03c0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
03d0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
03e0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
03f0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0400   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0410   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0420   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0430   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0440   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0450   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0460   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0470   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0480   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0490   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
04a0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
04b0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
04c0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
04d0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
04e0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
04f0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0500   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0510   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0520   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0530   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0540   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0550   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0560   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0570   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0580   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
0590   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
05a0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
05b0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
05c0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
05d0   68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68  hhhhhhhhhhhhhhhh
05e0   68 68 68 68 68 68 68 68 68 68                    hhhhhhhhhh

and here is the output from the code:

read: 582
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-UA-Compatible: IE=edge,chrome=1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
X-Frame-Options: ALLOW-FROM *
X-Application-Context: application
Cache-Control: no-store, no-cache, must-revalidate, max-age=0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Content-Type: application/javascript;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 26 Sep 2014 09:26:17 GMT

----

How do I get the remaining data?

Edit: If I decrease the receive buffer size and write a single byte to the outputstream after reading, I seem to receive the missing data. Why is that?


Answer:

Turns out that it was caused by my virusscanner's "online shield". As soon as I turned off AVG AntiVirus Business Editions' online shield, my missing data started coming through. Looks like it does not like streaming data.

Question:

consider my HAProxy config here:

global
    chroot /var/lib/haproxy
    user haproxy
    group haproxy

defaults
    timeout connect 10s
    timeout client 50s
    timeout server 50s

frontend fe_https_tomcat
    mode tcp
    bind *:443 ssl crt /path/cert.pem alpn h2,http/1.1
    default_backend be_tomcat

backend be_tomcat
    mode tcp
    server localhost localhost:8081 check

The issue I have is that WebSocket do not seem to get through. My guess was that in tcp mode everything would pass through. Looks like it doesn't ... :-)

The server responds with an error 403 when the WebSocket connection is getting established.

Note that with the following http-mode setup, the WebSocket just works:

frontend fe_http_8080
    mode http
    bind *:8080
    default_backend be_tomcat_8080

backend be_tomcat_8080
    mode http
    server localhost localhost:8081 check

Note that I need tcp-mode to have http/2 working.


Answer:

The issue was not related to HAProxy at the end, but to the WebSocket setup in Spring. This fixed it:

-registry.addHandler(webSocketHandler, "/ws");
+registry.addHandler(webSocketHandler, "/ws").setAllowedOrigins("*");