一·什么是websocket
WebSocket協議相比較於HTTP協議成功握手后可以多次進行通訊,直到連接被關閉。但是WebSocket中的握手和HTTP中的握手兼容,它使用HTTP中的Upgrade協議頭將連接從HTTP升級到WebSocket。這使得WebSocket程序可以更容易的使用現已存在的基礎設施。
WebSocket工作在HTTP的80和443端口並使用前綴ws://或者wss://進行協議標注,在建立連接時使用HTTP/1.1的101狀態碼進行協議切換,當前標准不支持兩個客戶端之間不借助HTTP直接建立Websocket連接。
二.創建基於Node的WebSocket服務
安裝node.js和npm
1
|
$ yum
install
nodejs npm
|
安裝ws和wscat模塊
ws是nodejs的WebSocket實現,我們借助它來搭建簡單的WebSocket Echo Server。
wscat是一個可執行的WebSocket客戶端,用來調試WebSocket服務是否正常。
1
|
npm
install
ws wscat
|
創建一個簡單的服務端
1
2
3
4
5
6
7
8
9
10
11
|
$ vim server.js
console.log(
"Server started"
);
var
Msg =
''
;
var
WebSocketServer = require(
'ws'
).Server
, wss =
new
WebSocketServer({port: 8010});
wss.on(
'connection'
,
function
(ws) {
ws.on(
'message'
,
function
(message) {
console.log(
'Received from client: %s'
, message);
ws.send(
'Server received from client: '
+ message);
});
});
|
運行服務端
1
2
|
$ node server.js
Server started
|
驗證服務端是否正常啟動
1
2
|
$ netstat -tlunp|grep 8010
tcp6 0 0 :::8010 :::* LISTEN 23864/nodejs
|
使用wscat做為客戶端測試
wscat命令默認安裝當前用戶目錄node_modules/wscat/目錄,我這里的位置是/root/node_modules/wscat/bin/wscat
輸入任意內容進行測試,得到相同返回則說明運行正常。
1
2
3
4
5
6
7
|
$
cd
/root/node_modules/wscat/bin/
$ .
/wscat
--connect ws:
//127
.0.0.1:8010
connected (press CTRL+C to quit)
> Hello
< Server received from client: Hello
> Welcome to www.hi-linux.com
< Server received from client: Welcome to www.hi-linux.com
|
三.使用Nginx對WebSocket進行反向代理
安裝Nginx
1
|
yum -y
install
nginx
|
配置Nginx Websocket
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
$ vim
/usr/local/nginx/conf/nginx
.conf
# 在http上下文中增加如下配置,確保Nginx能處理正常http請求。
http{
map $http_upgrade $connection_upgrade {
default upgrade;
''
close;
}
upstream websocket {
#ip_hash;
server localhost:8010;
server localhost:8011;
}
# 以下配置是在server上下文中添加,location指用於websocket連接的path。
server {
listen 80;
server_name localhost;
access_log
/var/log/nginx/yourdomain
.log;
location / {
proxy_pass http:
//websocket
;
proxy_read_timeout 300s;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
}
|
最重要的就是在反向代理的配置中增加了如下兩行,其它的部分和普通的HTTP反向代理沒有任何差別。
1
2
|
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
|
這里面的關鍵部分在於HTTP的請求中多了如下頭部:
1
2
|
Upgrade: websocket
Connection: Upgrade
|
這兩個字段表示請求服務器升級協議為WebSocket。服務器處理完請求后,響應如下報文# 狀態碼為101
1
2
3
|
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: upgrade
|
告訴客戶端已成功切換協議,升級為Websocket協議。握手成功之后,服務器端和客戶端便角色對等,就像普通的Socket一樣,能夠雙向通信。不再進行HTTP的交互,而是開始WebSocket的數據幀協議實現數據交換。
這里使用map指令可以將變量組合成為新的變量,會根據客戶端傳來的連接中是否帶有Upgrade頭來決定是否給源站傳遞Connection頭,這樣做的方法比直接全部傳遞upgrade更加優雅。
默認情況下,連接將會在無數據傳輸60秒后關閉,proxy_read_timeout參數可以延長這個時間或者源站通過定期發送ping幀以保持連接並確認連接是否還在使用。
啟動nginx
1
|
/etc/init.d/nginx start
|
試通過Nginx訪問WebSocket服務
1
2
3
4
5
6
7
|
$
cd
/root/node_modules/wscat/bin/
$ .
/wscat
--connect ws:
//192
.168.2.210
connected (press CTRL+C to quit)
> Hello Nginx
< Server received from client: Hello Nginx
> Welcome to www.hi-linux.com
< Server received from client: Welcome to www.hi-linux.com
|
測試成功,ok
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。