WebSocket-Over-HTTP Protocol
WebSocket-Over-HTTP Protocol
The WebSocket-Over-HTTP protocol is a simple, text-based protocol for gatewaying between a WebSocket client and a conventional HTTP server.
Why?
Pushpin’s Generic Realtime Intermediary Protocol (GRIP) enables out-of-band message injection into WebSocket connections. Normally, using GRIP with WebSockets requires a WebSocket connection on both sides of the proxy:
Client <--WS--> GRIP Proxy <--WS--> Server
The GRIP Proxy is a publish/subscribe service. When the server has data to send spontaneously, it does not use its WebSocket connection to send the data. Rather, it uses an out-of-band publish command to the proxy (usually via HTTP POST). This means that the WebSocket connection between the proxy and the server is used almost exclusively for servicing incoming requests from the client.
If the communication path between the proxy and the server only needs to handle request/response interactions, then HTTP becomes a viable alternative to a WebSocket:
Client <--WS--> GRIP Proxy <--HTTP--> Server
Using HTTP for communication between the proxy and server may be easier to maintain and scale since HTTP server tools are well established. Plus, if the server is merely doing stateless RPC processing, then HTTP is arguably a respectable choice for this tier in the service.
Of course, the usefulness of this gatewaying is entirely dependent on the server having a way to send data to clients out-of-band. As such, it is recommended that the WebSocket-Over-HTTP protocol be used in combination with GRIP. Note, however, that the WebSocket-Over-HTTP protocol does not explicitly depend on GRIP.
Protocol
The gateway and server exchange WebSocket “events” via HTTP requests and responses. The following events are defined:
OPEN- WebSocket negotiation request or acknowledgement.TEXT,BINARY- Messages with content.PING,PONG- Ping and pong messages.CLOSE- Close message with 16-bit close code.DISCONNECT- Indicates connection closed uncleanly or does not exist.
Events are encoded in a format similar to HTTP chunked transfer encoding:
TEXT B\r\n
hello world\r\n
The format is the name of the event, a space, the hexidecimal encoding of the content size, a carriage return and newline, the content bytes, and finally another carriage return and newline.
For events with no content, the size and content section can be omitted:
OPEN\r\n
Events with content are TEXT, BINARY, and CLOSE. Events without content are OPEN, PING, PONG, and DISCONNECT.
An event that should not contain content MAY be encoded with content. Receivers should ignore such content. For example, this is legal:
OPEN 0\r\n
\r\n
One or more encoded events are then concatenated and placed in the body of an HTTP request or response, with content type application/websocket-events.
Example
Gateway opens connection:
POST /target HTTP/1.1
Connection-Id: b5ea0e11
Content-Type: application/websocket-events
[... any headers included by the client WebSocket handshake ...]
OPEN\r\n
Server accepts connection:
HTTP/1.1 200 OK
Content-Type: application/websocket-events
[... any headers to include in the WebSocket negotiation response ...]
OPEN\r\n
Gateway relays message from client:
POST /target HTTP/1.1
Connection-Id: b5ea0e11
Content-Type: application/websocket-events
TEXT 5\r\n
hello\r\n
Server responds with two messages:
HTTP/1.1 200 OK
Content-Type: application/websocket-events
TEXT 5\r\n
world\r\n
TEXT 1C\r\n
here is another nice message\r\n
Gateway relays a close message:
POST /target HTTP/1.1
Connection-Id: b5ea0e11
Content-Type: application/websocket-events
CLOSE 2\r\n
[... binary status code ...]\r\n
Server sends a close message back:
HTTP/1.1 200 OK
Content-Type: application/websocket-events
CLOSE 2\r\n
[... binary status code ...]\r\n
State Management
Headers of the initial WebSocket negotiation request MUST be replayed with every request made by the gateway. This means that if the client uses cookies or other headers for authentication purposes, the server will receive this data with every message.
The gateway includes a Connection-Id header which uniquely identifies a particular client connection. Servers that need to track connections can use this. In most cases, though, servers should not have to care about connections.
It is possible to bind metadata to the connection via a Set-Meta-* header. This works similar to a cookie. The server can set a field that the gateway should echo back on all subsequent requests.
For example, a client supplies a cookie which the gateway relays across during connect:
POST /target HTTP/1.1
Connection-Id: b5ea0e11
Content-Type: application/websocket-events
Cookie: [... auth info ...]
OPEN\r\n
The server accepts the connection and binds a User field based on the cookie:
HTTP/1.1 200 OK
Content-Type: application/websocket-events
Set-Meta-User: alice
OPEN\r\n
Now, any further requests from the gateway will include a Meta-User header:
POST /target HTTP/1.1
Connection-Id: b5ea0e11
Meta-User: alice
Content-Type: application/websocket-events
TEXT 5\r\n
hello\r\n
Security note: gateways MUST NOT relay any headers from the client that are prefixed with Meta-. This prevents the client from spoofing metadata bindings. Additionally, the server needs to ensure that an incoming request came from a gateway before trusting its Meta-* headers.
Notes
- The first request MUST contain an OPEN event as the first event.
- The first response MUST contain an OPEN event as the first event.
- If the server tracks connections and no longer considers the connection to exist, it should respond with DISCONNECT. In most cases, servers will not track connections, though.
- Gateway should only have one outstanding request per client connection. This ensures in-order delivery.
- DISCONNECT event only sent if connection was not closed cleanly. With clean close, disconnect is implied.
- Within this protocol alone, the server has no way to talk to the client outside of responding to incoming requests.
- Gateway can send an empty request to keep-alive the current connection. The gateway shall consider an empty response to be a keep-alive from the server. The server enables keep-alives by providing a Keep-Alive-Interval response header.
WebSocket-Over-HTTP Protocol的更多相关文章
- spring websocket源码分析
什么是websocket? 摘录于wiki[1]: WebSocket is a protocol providing full-duplex communication channels over ...
- 学习html5的WebSocket连接
1.什么是WebSocket WebSocket 是一种自然的全双工.双向.单套接字连接.使用WebSocket,你的HTTP 请求变成打开WebSocket 连接(WebSocket 或者WebSo ...
- webSocket学习与应用
非原创,版权归原作者所有http://www.cnblogs.com/shizhouyu/p/4975409.html 1.什么是WebSocket WebSocket 是一种自然的全双工.双向.单套 ...
- websocket学习和使用
1)WebSocket介绍 HTML5 Web Sockets规范定义了Web Sockets API,支持页面使用Web Socket协议与远程主机进行全双工的通信.它引入了WebSocket接口并 ...
- 实现WebSocket和WAMP协议的开源库WampSharp
Websocket Application Messaging Protocol 协议:https://github.com/wamp-proto/wamp-proto 1. 基础档案 引入: WAM ...
- WebSocket【转】
1.什么是WebSocket WebSocket 是一种自然的全双工.双向.单套接字连接.使用WebSocket,你的HTTP 请求变成打开WebSocket 连接(WebSocket 或者WebSo ...
- EMQ ---websocket
简介 近年来随着 Web 前端的快速发展,浏览器新特性层出不穷,越来越多的应用可以在浏览器端或通过浏览器渲染引擎实现,Web 应用的即时通信方式 WebSocket 得到了广泛的应用. WebSock ...
- websocket原理和基于c/c++实现的websocket协议栈(更新中)
参考: 博客1:http://blog.sina.com.cn/s/blog_bf397e780102w25k.html https://www.cnblogs.com/barrywxx/p/7412 ...
- HTML5入门总结 HTML5API
w3cshools MDN英文 MDN中文 HTML5 HTML5 is the latest evolution of the standard that defines HTML. The t ...
- 5 Protocols For Event-Driven API Architectures
The internet is a system of communication, and as such, the relationship between client and server, ...
随机推荐
- loadrunner json参数化
因为json格式有{},所以LR参数化时如果也用默认的{}的话,会冲突,这样脚本运行时就无法识别,导致不能正确的读取参数化文件里的内容,此时把参数化的{}改成其他符号即可,比如<>
- Capjoint
前期知识储备: 现如今的全球台站分布情况:在全球大陆的分布并不均衡.在美国加利福尼亚.日本.中国台湾等地区,台站分布密集,数字化程度高.然而在世界很多地区发生的地震,往往在300km范围内仅有2到3个 ...
- vs2017 乱码
vs2017默认编码方式并不是UTF-8,似乎是UTF-16,当我们使用中文时,经常会发生乱码. 解决方法:工具->扩展和更新->联机,然后搜索ForceUTF8 我这里已经安装了,没安装 ...
- java基础语法学习DayOne
一.关键字 1.概述:被java语言赋予特定含义的单词 2.特点:组成关键字的字母全部为小写 二.标识符 1.概述:给类.接口.方法.变量起名字时使用的字符序列 2.规则:只能使用英文大小写字母.数字 ...
- L300 3月英语课下
重音 句中的实词(内容词)要被重读,读得重.长一点.句中的虚词(功能词)要被轻读或弱读,读得轻.短一点口语交流中,当虚词的在句中起到重要的表意作用时,会被重读. 连读 连读:把一个单词的尾音同下一个单 ...
- day 51
一 window对象 window 对象表示一个浏览器窗口. 在客户端 JavaScript 中,Window 对象是全局对象,所有的表达式都在当前的环境中计算.也就是说,要引用当前窗口根本不需要特殊 ...
- 用python从符合一定格式的txt文档中逐行读取数据并按一定规则写入excel(openpyxl支持Excel 2007 .xlsx格式)
前几天接到一个任务,从gerrit上通过ssh命令获取一些commit相关的数据到文本文档中,随后将这些数据存入Excel中.数据格式如下图所示 观察上图可知,存在文本文档中的数据符合一定的格式,通过 ...
- 看我怎么扒掉CSDN首页的底裤(python selenium+phantomjs爬取CSDN首页内容)
这里只是学习一下动态加载页面内容的抓取,并不适用于所有的页面. 使用到的工具就是python selenium和phantomjs,另外调试的时候还用了firefox的geckodriver.exe. ...
- Spring Boot 揭秘与实战(二) 数据存储篇 - Redis
文章目录 1. 环境依赖 2. 数据源 2.1. 方案一 使用 Spring Boot 默认配置 2.2. 方案二 手动创建 3. 使用 redisTemplate 操作4. 总结 3.1. 工具类 ...
- Sphinx将python代码注释生成文档
安装 使用pip进行安装: pip install sphinx 初始化 进入你代码所在的目录,输入: sphinx-quickstart 下图:PRD是代码所在目录,生成的文档保存目录设成doc ...