简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM