12 Kasım 2020 Perşembe

SpringWebSocket WebSocket, Stomp, SockJS İlişkisi

Giriş
Bu üçlünün ilişkisi şöyle. Bu üçlünün ayarlandığı AbstractWebSocketMessageBrokerConfigurer sınıfı yazısına bakabilirsiniz.

WebSocket  - Transport
WebSocket TCP değildir ancak TCP gibi düşünülebilir. İstemciden sunucuya çift yönlü bir socket bağlantısı açar. Açıklaması şöyle
The WebSocket API enables web applications to handle bidirectional communications with server-side process in a straightforward way. Developers have been using XMLHttpRequest ("XHR") for such purposes, but XHR makes developing web applications that communicate back and forth to the server unnecessarily complex. XHR is basically asynchronous HTTP, and because you need to use a tricky technique like long-hanging GET for sending data from the server to the browser, simple tasks rapidly become complex. As opposed to XMLHttpRequest, WebSockets provide a real bidirectional communication channel in your browser. Once you get a WebSocket connection, you can send data from browser to server by calling a send() method, and receive data from server to browser by an onmessage event handler.

In addition to the new WebSocket API, there is also a new protocol (the "WebSocket Protocol") that the browser uses to communicate with servers. The protocol is not raw TCP because it needs to provide the browser's "same-origin" security model. It's also not HTTP because WebSocket traffic differers from HTTP's request-response model. WebSocket communications using the new WebSocket protocol should use less bandwidth because, unlike a series of XHRs and hanging GETs, no headers are exchanged once the single connection has been established. To use this new API and protocol and take advantage of the simpler programming model and more efficient network traffic, you do need a new server implementation to communicate with.
Stomp - Payload
Stomp bir  metin (text) protokolü. Transport protokolü olarak WebSocket kullanmak zorunda değil. Stomp mesajı (frame) şu 3 alandan oluşur
command ("CONNECT", "SEND", etc.)
headers JavaScript object
body
SockJS - Transport İçin B Planı Sağlar
Direkt WebSocket kullanmak yerine SockJS kullanılırsa, WebSocket'in mevcut olmadığı durumlarda da iletişim sağlanabilir. Açıklaması şöyle
Under the hood SockJS tries to use native WebSockets first. If that fails it can use a variety of browser-specific transport protocols and presents them through WebSocket-like abstractions.

SockJS is intended to work for all modern browsers and in environments which don't support the WebSocket protocol -- for example, behind restrictive corporate proxies.
Farklı protokoller şunlar olabilir
xhr-streaming, xhr-polling, and so on












Hiç yorum yok:

Yorum Gönder