简体   繁体   English

netty websocket连接通过java客户端

[英]netty websocket connection via java client

Is it possible to create a websocket connection in java code, without the handshake request? 是否可以在没有握手请求的情况下在java代码中创建websocket连接?

I know how to create one with a handhsake request using the following: 我知道如何用handhsake请求创建一个使用以下内容:

  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. 没有WebSocket握手,您无法创建WebSocket连接。 The WebSocket handshake and framing is a critical part of the protocol. WebSocket握手和成帧是协议的关键部分。 The handshake is HTTP compatible and allows WebSockets to more easily interact with existing web infrastructure. 握手是HTTP兼容的,允许WebSockets更容易地与现有的Web基础结构交互。 Among other things, the handshake adds security mechanisms and allows Cross Origin Resource Sharing ( CORS ). 除此之外,握手还增加了安全机制并允许跨源资源共享( CORS )。

After the handshake, each WebSocket frame is still not raw data. 握手后,每个WebSocket帧仍然不是原始数据。 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. WebSocket是一种基于消息的协议,因此帧头包含消息描述,帧长度,消息类型(二进制,文本,ping等)等。此外,必须使用正在运行的XOR来屏蔽从客户端(浏览器)到服务器的数据面具。 This is to avoid a theoretical vulnerability in HTTP intermediaries (proxies, HTTP caches, etc). 这是为了避免HTTP中介(代理,HTTP缓存等)中的理论漏洞。

Don't be misled by the "Socket" in the name. 不要被名称中的“Socket”误导。 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. WebSockets具有原始TCP套接字的许多优点,例如全双工,双向,长寿命和低延迟,但它是基于消息的传输协议,分层在原始TCP套接字上并使用HTTP友好握手。

See the official IETF 6455 WebSocket spec for more details. 有关更多详细信息,请参阅官方IETF 6455 WebSocket规范

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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