user1555190
user1555190

Reputation: 3233

netty websocket connection via java client

Is it possible to create a websocket connection in java code, without the handshake request?

I know how to create one with a handhsake request using the following:

  String request = "GET " + path + " HTTP/1.1\r\n"
            + "Upgrade: WebSocket\r\n" + "Connection: Upgrade\r\n"
            + "Host: " + host + "\r\n" + "Origin: " + origin + "\r\n"
            + extraHeaders.toString() + "\r\n";

But i want o avoid the above, and once i open a socket connection, just want to send frames down the channel?.. is this possible?

Upvotes: 1

Views: 1163

Answers (1)

kanaka
kanaka

Reputation: 73091

You cannot create a WebSocket connection without the WebSocket handshake. The WebSocket handshake and framing is a critical part of the protocol. The handshake is HTTP compatible and allows WebSockets to more easily interact with existing web infrastructure. Among other things, the handshake adds security mechanisms and allows Cross Origin Resource Sharing (CORS).

After the handshake, each WebSocket frame is still not raw data. WebSocket is a message based protocol so the frame headers contain message delineation, frame length, message type (binary, text, ping, etc), etc. Also, data from the client (browser) to the server must be masked using a running XOR mask. This is to avoid a theoretical vulnerability in HTTP intermediaries (proxies, HTTP caches, etc).

Don't be misled by the "Socket" in the name. WebSockets has many benefits of raw TCP sockets such as being full-duplex, bi-directional, long-lived and low latency, but it is a message based transport protocol layered on raw TCP sockets and using an HTTP friendly handshake.

See the official IETF 6455 WebSocket spec for more details.

Upvotes: 3

Related Questions