一、WebSocket請求頭信息
WebSocket是一種在單個TCP連接上提供雙向通信的Web技術。WebSocket請求頭是提交到服務器的初始信息。以下是WebSocket請求頭中的一些字段:
Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: xx== // 隨機生成的用於表示該連接的唯一性的字符串 Host: echo.websocket.org Origin: http://www.example.com Sec-WebSocket-Protocol: chat, superchat // 指定了用於通信的子協議 Sec-WebSocket-Version: 13 // WebSocket的版本號
以上每個字段的意義和作用可以通過在文檔中查找相應的屬性值獲得。
二、WebSocket設置請求頭
在實際運用中,WebSocket請求頭中的某些字段需要被設置為特定的值。下面是一個使用JavaScript訪問WebSocket並設置請求頭的示例:
var socket = new WebSocket('ws://echo.websocket.org'); socket.binaryType = 'arraybuffer'; socket.addEventListener('open', function (event) { socket.send('Hello, server!'); }); socket.addEventListener('message', function (event) { console.log(event.data); }); socket.addEventListener('close', function (event) { console.log('WebSocket closed'); }); socket.addEventListener('error', function (event) { console.error(event); });
三、WebSocket端口
WebSocket通過TCP端口進行通信。默認端口是80或443。如果使用的端口不同,應該在WebSocket的URL中指定它。例如:
var socket = new WebSocket('ws://example.com:12345');
四、WebSocket和POST
在使用WebSocket時,可以使用標準的HTTP POST請求來向服務器發送消息。具體方式是在WebSocket請求頭中增加“Content-Type: application/json”字段,然後在WebSocket的消息事件中使用WebSocket.send()方法發送POST請求。
var socket = new WebSocket('ws://example.com'); socket.addEventListener('open', function (event) { socket.send(JSON.stringify({ method: 'POST', path: '/api/data', body: { foo: 'bar' } })); });
五、WebSocket發送JSON
由於WebSocket是基於TCP的傳輸協議,可以傳輸任何二進制數據,包括JSON對象。例如:
var socket = new WebSocket('ws://example.com'); socket.addEventListener('open', function (event) { socket.send(JSON.stringify({ message: 'Hello, server!', timestamp: Date.now(), isImportant: true })); }); socket.addEventListener('message', function (event) { console.log(JSON.parse(event.data)); });
六、WebSocket C庫
WebSocket C庫是一個用於實現WebSocket協議的開源庫,開發人員可以使用它自定義WebSocket協議。以下是C語言中使用WebSocket C庫的示例:
#include <websocket.h> #include <stdio.h> void on_message(websocket_t ws, char *msg) { printf("Message: %s\n", msg); } int main() { websocket_t ws = websocket_new("ws://echo.websocket.org", NULL); websocket_on(ws, MESSAGE, on_message); websocket_run(ws); websocket_free(ws); return 0; }
七、WebSocket登錄
在使用WebSocket時,有時需要使用登錄驗證。例如,可以在請求頭中增加Authorization字段,並在值中添加Bearer token。
var socket = new WebSocket('ws://example.com'); socket.addEventListener('open', function (event) { socket.send(JSON.stringify({ Authorization: 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', message: 'Hello, server!' })); });
八、WebSocket前端怎麼寫
WebSocket前端代碼的編寫和普通的JavaScript代碼非常類似。首先創建一個WebSocket實例,連接到服務器,然後添加必要的事件偵聽器。
var socket = new WebSocket('ws://example.com'); socket.addEventListener('open', function (event) { console.log('WebSocket connected'); }); socket.addEventListener('message', function (event) { console.log('Received message:', event.data); }); socket.addEventListener('close', function (event) { console.log('WebSocket closed'); }); socket.addEventListener('error', function (event) { console.error('WebSocket error:', event); });
九、WebSocket和HTTP
WebSocket可以使用任何可用的HTTP端口(如80和443)來進行通信。在客戶端向服務器發送HTTP請求時,WebSocket使用HTTP連接。然後,服務器返回一個HTTP 101交換握手。這是一個初始頭文件,通知客戶端,WebSocket連接已建立。從那一刻起,客戶端和服務器之間的通信就可以開始了。下面是一個WebSocket交換握手的示例:
GET / HTTP/1.1 Host: example.com Connection: Upgrade Upgrade: websocket Sec-WebSocket-Key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx // 隨機生成的用於表示該連接的唯一性的字符串 Sec-WebSocket-Version: 13
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/238148.html