The WebSocket Protocol enables two-way communication between a client running untrusted code in a controlled environment to a remote hos" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網(wǎng)站運營 > 初識WebSocket協(xié)議

初識WebSocket協(xié)議

時間:2023-07-03 01:09:01 | 來源:網(wǎng)站運營

時間:2023-07-03 01:09:01 來源:網(wǎng)站運營

初識WebSocket協(xié)議:

1.什么是WebSocket協(xié)議

RFC6455文檔的表述如下:

The WebSocket Protocol enables two-way communication between a client running untrusted code in a controlled environment to a remote host that has opted-in to communications from that code. The security model used for this is the origin-based security model commonly used by web browsers. The protocol consists of an opening handshake followed by basic message framing, layered over TCP. The goal of this technology is to provide a mechanism for browser-based applications that need two-way communication with servers that does not rely on opening multiple HTTP connections.

大意是說WebSocket是一個基于TCP協(xié)議的全雙工的應用層協(xié)議,主要用于Web瀏覽器,其目的是使基于瀏覽器、需要全雙工通信的web應用不再依賴于多個HTTP連接。

2.應用WebSocket

設(shè)想這樣一個場景,一個基于B/S架構(gòu)的應用,其功能是服務器主動向瀏覽器定時發(fā)送一個消息,應該怎么做?由于HTTP協(xié)議只能由客戶端發(fā)起請求,服務器端響應請求建立連接,所以通過HTTP協(xié)議實現(xiàn)服務器主動推送消息有一定的難度,可以通過瀏覽器客戶端定時向服務器發(fā)送HTTP請求來實現(xiàn),Comet就是基于這種方式,實際上這并不是真正的“服務器主動”。然而依靠WebSocket,我們能夠輕易的做到這一點。

現(xiàn)在WebSocket的支持情況如下:

1.服務器端

  1. IIS 7.0+
  2. Tomcat 7.0.5+
  3. Jetty t.0+
  4. WebLogic 12c
  5. WebSphere 8.0+
2.瀏覽器端

  1. Chrome 4+
  2. FireFox 5+
  3. IE 10+
  4. Safari IOS 5+
  5. Android Browser Android 4.5+
下面我們將利用WebSocket實現(xiàn)一個簡單的java webapp,其功能是服務器主動向瀏覽器發(fā)送三條消息。服務器為Tomcat 8.5.4,除此之外,要為我們的app引入支持WebSocket的jar包---websocket-api.jar。如需觀察其效果,請移步http://139.129.95.147/TestWebSocket/。

代碼如下:

前端:index.html

<!DOCTYPE html><html><head> <title>Testing websockets</title></head><body><div> <input type="submit" value="Start" onclick="start()" /></div><div id="messages"></div><script type="text/javascript"> var webSocket = new WebSocket('ws://139.129.95.147/TestWebSocket/websocket'); webSocket.onerror = function(event) { onError(event) }; webSocket.onopen = function(event) { onOpen(event) }; webSocket.onmessage = function(event) { onMessage(event) }; function onMessage(event) { document.getElementById('messages').innerHTML += '<br />' + event.data; } function onOpen(event) { document.getElementById('messages').innerHTML = 'Connection established'; } function onError(event) { alert(event.data); } function start() { webSocket.send('hello'); return false; }</script></body></html>后端:

import java.io.IOException;import javax.websocket.OnClose;import javax.websocket.OnMessage;import javax.websocket.OnOpen;import javax.websocket.Session;import javax.websocket.server.ServerEndpoint;@ServerEndpoint("/websocket")public class TestWebSocket { @OnMessage public void onMessage(String message, Session session) throws IOException, InterruptedException { // Print the client message for testing purposes System.out.println("Received: " + message); // Send the first message to the client session.getBasicRemote().sendText("This is the first server message"); // Send 3 messages to the client every 5 seconds int sentMessages = 0; while(sentMessages < 3){ Thread.sleep(5000); session.getBasicRemote(). sendText("This is an intermediate server message. Count: " + sentMessages); sentMessages++; } // Send a final message to the client session.getBasicRemote().sendText("This is the last server message"); } @OnOpen public void onOpen() { System.out.println("Client connected"); } @OnClose public void onClose() { System.out.println("Connection closed"); }}在Tomcat中使用WebSocket,首先需要在服務器端建立一個endpoint,語法為

@ServerEndpoint("/websocket")然后在前端根據(jù)這個endpoint的url獲取一個WebSocket對象,然后調(diào)用其相關(guān)方法即可。由于代碼較為簡單,在本文中不在贅述,我會在后續(xù)文章中詳細分析。

3.WebSocket和TCP、HTTP的關(guān)系

WebSocket是一個獨立的、基于TCP協(xié)議的應用層協(xié)議,它和HTTP協(xié)議唯一的關(guān)系就是WebSocket協(xié)議的握手建立連接的過程是由HTTP服務器實現(xiàn)的,并且HTTP服務器將之視為HTTP協(xié)議的一個升級版。

關(guān)鍵詞:協(xié)議

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點擊下載Chrome瀏覽器
關(guān)閉