天天看點

WebSocket+STOMP+SockJS

WebSocket

https://www.w3.org/TR/websockets/

The WebSocket protocol defines two types of messages, text and binary, but their content is undefined. It’s expected that the client and server may agree on using a sub-protocol (i.e. a higher-level protocol) to define message semantics. While the use of a sub-protocol with WebSocket is completely optional either way client and server will need to agree on some kind of protocol to help interpret messages.

WebSocket sub-protocols

the WebSocket RFC defines the use of sub-protocols

https://tools.ietf.org/html/rfc6455#section-1.9

STOMP

STOMP is a simple text-orientated messaging protocol.

STOMP is widely supported and well suited for use over WebSocket and over the web.

STOMP是一種簡單的消息協定,非常合适用作WebSocket的子協定

http://jmesnil.net/stomp-websocket/doc/

SockJS

https://github.com/sockjs/sockjs-client

使用SockJS作為用戶端,當浏覽器不支援websocket時,會使用長輪詢等方式。

The SockJS client begins by sending “GET /info” to obtain basic information from the server. After that it must decide what transport to use. If possible WebSocket is used. If not, in most browsers there is at least one HTTP streaming option and if not then HTTP (long) polling is used.

Chrome 可以在引用sockjs檔案前禁用websocket,進而看到長輪詢的效果。

window.WebSocket = null;
           

繼續閱讀