闆惰窛绂绘帴瑙ebsocket馃殌

Posted 涓変綑

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了闆惰窛绂绘帴瑙ebsocket馃殌相关的知识,希望对你有一定的参考价值。

浠€涔堟槸WebSocket

瀹氫箟

Websocket鏄竴涓寔涔呭寲鐨勭綉缁滈€氫俊鍗忚锛屽彲浠ュ湪鍗曚釜 TCP 杩炴帴涓婅繘琛?code>鍏ㄥ弻宸ラ€氳锛屾病鏈変簡Request鍜?code>Response鐨勬蹇碉紝涓よ€呭湴浣嶅畬鍏ㄥ钩绛夛紝杩炴帴涓€鏃﹀缓绔嬶紝瀹㈡埛绔拰鏈嶅姟绔箣闂村疄鏃跺彲浠ヨ繘琛屽弻鍚戞暟鎹紶杈?/p>

鍏宠仈鍜屽尯鍒?/h4>
  • HTTP
  1. HTTP鏄潪鎸佷箙鐨勫崗璁紝瀹㈡埛绔兂鐭ラ亾鏈嶅姟绔殑澶勭悊杩涘害鍙兘閫氳繃涓嶅仠鍦颁娇鐢?Ajax杩涜杞鎴栬€呴噰鐢?long poll 鐨勬柟寮忔潵锛屼絾鏄墠鑰呭鏈嶅姟鍣ㄥ帇鍔涘ぇ锛屽悗鑰呭垯浼氬洜涓轰竴鐩寸瓑寰匯esponse閫犳垚闃诲
  2. 铏界劧http1.1榛樿寮€鍚簡keep-alive闀胯繛鎺ヤ繚鎸佷簡杩欎釜TCP閫氶亾浣垮緱鍦ㄤ竴涓狧TTP杩炴帴涓紝鍙互鍙戦€佸涓猂equest锛屾帴鏀跺涓猂esponse锛屼絾鏄竴涓猺equest鍙兘鏈変竴涓猺esponse銆傝€屼笖杩欎釜response涔熸槸琚姩鐨勶紝涓嶈兘涓诲姩鍙戣捣銆?/li>
  3. websocket铏界劧鏄嫭绔嬩簬HTTP鐨勪竴绉嶅崗璁紝浣嗘槸websocket蹇呴』渚濊禆 HTTP 鍗忚杩涜涓€娆?code>鎻℃墜(鍦ㄦ彙鎵嬮樁娈垫槸涓€鏍风殑)锛屾彙鎵嬫垚鍔熷悗锛屾暟鎹氨鐩存帴浠?TCP閫氶亾浼犺緭锛屼笌 HTTP 鏃犲叧浜嗭紝鍙互鐢ㄤ竴寮犲浘鐞嗚В涓よ€呮湁浜ら泦锛屼絾鏄苟涓嶆槸鍏ㄩ儴銆?
  • socket
  1. socket涔熻绉颁负濂楁帴瀛?/code>锛屼笌HTTP鍜學ebSocket涓嶄竴鏍凤紝socket涓嶆槸鍗忚锛屽畠鏄湪绋嬪簭灞傞潰涓婂浼犺緭灞傚崗璁紙鍙互涓昏鐞嗚В涓篢CP/IP锛夌殑鎺ュ彛灏佽銆傚彲浠ョ悊瑙d负涓€涓兘澶熸彁渚涚瀵圭鐨勯€氫俊鐨勮皟鐢ㄦ帴鍙o紙API锛?/li>
  2. 瀵逛簬绋嬪簭鍛樿€岃█锛屽叾闇€瑕佸湪 A 绔垱寤轰竴涓?socket 瀹炰緥锛屽苟涓鸿繖涓疄渚嬫彁渚涘叾鎵€瑕佽繛鎺ョ殑 B 绔殑 IP 鍦板潃鍜岀鍙e彿锛岃€屽湪 B 绔垱寤哄彟涓€涓?socket 瀹炰緥锛屽苟涓旂粦瀹氭湰鍦扮鍙e彿鏉ヨ繘琛岀洃鍚€傚綋 A 鍜?B 寤虹珛杩炴帴鍚庯紝鍙屾柟灏卞缓绔嬩簡涓€涓瀵圭鐨?TCP 杩炴帴锛屼粠鑰屽彲浠ヨ繘琛屽弻鍚戦€氫俊銆俉ebSocekt鍊熼壌浜?socket 鐨勬€濇兂锛屼负 client 鍜?server 涔嬮棿鎻愪緵浜嗙被浼肩殑鍙屽悜閫氫俊鏈哄埗

搴旂敤鍦烘櫙

WebSocket鍙互鍋氬脊骞曘€佹秷鎭闃呫€佸鐜╁娓告垙銆佸崗鍚岀紪杈戙€佽偂绁ㄥ熀閲戝疄鏃舵姤浠枫€佽棰戜細璁€佸湪绾挎暀鑲层€佽亰澶╁绛夊簲鐢ㄥ疄鏃剁洃鍚湇鍔$鍙樺寲

Websocket鎻℃墜

  • Websocket鎻℃墜璇锋眰鎶ユ枃锛?/li>
GET聽/chat聽HTTP/1.1
Host:聽server.example.com
Upgrade:聽websocket
Connection:聽Upgrade
Sec-WebSocket-Key:聽x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol:聽chat,聽superchat
Sec-WebSocket-Version:聽13
Origin:聽http://example.com
澶嶅埗浠g爜

涓嬮潰鏄笌浼犵粺 HTTP 鎶ユ枃涓嶅悓鐨勫湴鏂癸細

Upgrade:聽websocket
Connection:聽Upgrade
澶嶅埗浠g爜

琛ㄧず鍙戣捣鐨勬槸 WebSocket 鍗忚

Sec-WebSocket-Key:聽x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol:聽chat,聽superchat
Sec-WebSocket-Version:聽13
澶嶅埗浠g爜

Sec-WebSocket-Key 鏄敱娴忚鍣ㄩ殢鏈虹敓鎴愮殑锛岄獙璇佹槸鍚﹀彲浠ヨ繘琛學ebsocket閫氫俊锛岄槻姝㈡伓鎰忔垨鑰呮棤鎰忕殑杩炴帴銆?/p>

Sec_WebSocket-Protocol 鏄敤鎴疯嚜瀹氫箟鐨勫瓧绗︿覆锛岀敤鏉ユ爣璇嗘湇鍔℃墍闇€瑕佺殑鍗忚

Sec-WebSocket-Version 琛ㄧず鏀寔鐨?WebSocket 鐗堟湰銆?/p>

  • 鏈嶅姟鍣ㄥ搷搴旓細
HTTP/1.1聽101聽Switching聽Protocols
Upgrade:聽websocket
Connection:聽Upgrade
Sec-WebSocket-Accept:聽HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol:聽chat
澶嶅埗浠g爜

101 鍝嶅簲鐮?/strong> 琛ㄧず瑕佽浆鎹㈠崗璁€?/p>

Connection: Upgrade 琛ㄧず鍗囩骇鏂板崗璁姹傘€?/p>

Upgrade: websocket 琛ㄧず鍗囩骇涓?WebSocket 鍗忚銆?/p>

Sec-WebSocket-Accept 鏄粡杩囨湇鍔″櫒纭锛屽苟涓斿姞瀵嗚繃鍚庣殑 Sec-WebSocket-Key銆傜敤鏉ヨ瘉鏄庡鎴风鍜屾湇鍔″櫒涔嬮棿鑳借繘琛岄€氫俊浜嗐€?/p>

Sec-WebSocket-Protocol 琛ㄧず鏈€缁堜娇鐢ㄧ殑鍗忚銆?/p>

鑷虫锛屽鎴风鍜屾湇鍔″櫒鎻℃墜鎴愬姛寤虹珛浜哤ebsocket杩炴帴锛孒TTP宸茬粡瀹屾垚瀹冩墍鏈夊伐浣滀簡锛屾帴涓嬫潵灏辨槸瀹屽叏鎸夌収Websocket鍗忚杩涜閫氫俊浜嗐€?/p>

鍏充簬Websocket

WebSocket蹇冭烦

鍙兘浼氭湁涓€浜涙湭鐭ユ儏鍐靛鑷碨OCKET鏂紑锛岃€屽鎴风鍜屾湇鍔$鍗翠笉鐭ラ亾锛岄渶瑕佸鎴风瀹氭椂鍙戦€佷竴涓?code>蹇冭烦 Ping 璁╂湇鍔$鐭ラ亾鑷繁鍦ㄧ嚎锛岃€屾湇鍔$涔熻鍥炲涓€涓?code>蹇冭烦 Pong鍛婅瘔瀹㈡埛绔嚜宸卞彲鐢紝鍚﹀垯瑙嗕负鏂紑

WebSocket鐘舵€?/h4>

WebSocket 瀵硅薄涓殑readyState灞炴€ф湁鍥涚鐘舵€侊細

  • 0: 琛ㄧず姝e湪杩炴帴
  • 1: 琛ㄧず杩炴帴鎴愬姛锛屽彲浠ラ€氫俊浜?/li>
  • 2: 琛ㄧず杩炴帴姝e湪鍏抽棴
  • 3: 琛ㄧず杩炴帴宸茬粡鍏抽棴锛屾垨鑰呮墦寮€杩炴帴澶辫触

WebSocket瀹炶返

鏈嶅姟绔帴鏀跺彂閫佹秷鎭?/h4>

WebSocket鐨勬湇鍔$閮ㄥ垎锛屾湰鏂囦細浠ode.js鎼缓

瀹夎express鍜岃礋璐e鐞哤ebSocket鍗忚鐨?code>ws锛?/p>

npm聽install聽express聽ws
澶嶅埗浠g爜

瀹夎鎴愬姛鍚庣殑package.json:

鎺ョ潃鍦ㄦ牴鐩綍鍒涘缓server.js鏂囦欢:

//寮曞叆express聽鍜屄爓s
const聽express聽=聽require(\'express\');
const聽SocketServer聽=聽require(\'ws\').Server;
//鎸囧畾寮€鍚殑绔彛鍙?const聽PORT聽=聽3000;
//聽鍒涘缓express锛岀粦瀹氱洃鍚?000绔彛锛屼笖璁惧畾寮€鍚悗鍦╟onsol涓彁绀?const聽server聽=聽express().listen(PORT,聽()聽=>聽console.log(`Listening聽on聽${PORT}`));
//聽灏唀xpress浜ょ粰SocketServer寮€鍚疻ebSocket鐨勬湇鍔?const聽wss聽=聽new聽SocketServer({聽server聽});
//褰撀燱ebSocket聽浠庡閮ㄨ繛鎺ユ椂鎵ц
wss.on(\'connection\',聽(ws)聽=>聽{
聽聽//杩炴帴鏃舵墽琛屾聽console聽鎻愮ず
聽聽console.log(\'Client聽connected\');
聽聽//聽瀵筸essage璁剧疆鐩戝惉锛屾帴鏀朵粠瀹㈡埛绔彂閫佺殑娑堟伅
聽聽ws.on(\'message\',聽(data)聽=>聽{
聽聽聽聽//data涓哄鎴风鍙戦€佺殑娑堟伅锛屽皢娑堟伅鍘熷皝涓嶅姩杩斿洖鍥炲幓
聽聽聽聽ws.send(data);
聽聽});
聽聽//聽褰揥ebSocket鐨勮繛鎺ュ叧闂椂鎵ц
聽聽ws.on(\'close\',聽()聽=>聽{
聽聽聽聽console.log(\'Close聽connected\');
聽聽});
});
澶嶅埗浠g爜

鎵цnode server.js鍚姩鏈嶅姟锛岀鍙f墦寮€鍚庝細鎵ц鐩戝惉鏃堕棿鎵撳嵃鎻愮ず锛岃鏄庢湇鍔″惎鍔ㄦ垚鍔?/p>

鍦ㄥ紑鍚疻ebSocket鍚庯紝鏈嶅姟绔細鍦╩essage涓洃鍚紝鎺ユ敹鍙傛暟data鎹曡幏瀹㈡埛绔彂閫佺殑娑堟伅锛岀劧鍚庝娇鐢╯end鍙戦€佹秷鎭?/p>

瀹㈡埛绔帴鏀跺彂閫佹秷鎭?/h4>

鍒嗗埆鍦ㄦ牴鐩綍鍒涘缓index.html鍜宨ndex.js鏂囦欢

  • index.html
<html>
聽聽<body>
聽聽聽聽<script聽src="./index.js"></script>
聽聽</body>
</html>
澶嶅埗浠g爜
  • index.js
//聽浣跨敤WebSocket鐨勫湴鍧€鍚戞湇鍔$寮€鍚繛鎺?let聽ws聽=聽new聽WebSocket(\'ws://localhost:3000\');
//聽寮€鍚悗鐨勫姩浣滐紝鎸囧畾鍦ㄨ繛鎺ュ悗鎵ц鐨勪簨浠?ws.onopen聽=聽()聽=>聽{
聽聽console.log(\'open聽connection\');
};
//聽鎺ユ敹鏈嶅姟绔彂閫佺殑娑堟伅
ws.onmessage聽=聽(event)聽=>聽{
聽聽console.log(event);
};
//聽鎸囧畾鍦ㄥ叧闂悗鎵ц鐨勪簨浠?ws.onclose聽=聽()聽=>聽{
聽聽console.log(\'close聽connection\');
};
澶嶅埗浠g爜

涓婇潰鐨?code>url灏辨槸鏈満node寮€鍚殑鏈嶅姟鍦板潃锛屽垎鍒寚瀹氳繛鎺ワ紙onopen锛夛紝鍏抽棴锛坥nclose锛夊拰娑堟伅鎺ユ敹锛坥nmessage锛夌殑鎵ц浜嬩欢锛岃闂甴tml锛屾墦鍗皐s淇℃伅

鎵撳嵃浜?code>open connection璇存槑杩炴帴鎴愬姛锛屽鎴风浼氫娇鐢?code>onmessage澶勭悊鎺ユ敹

鍏朵腑event鍙傛暟鍖呭惈杩欐娌熼€氱殑璇︾粏淇℃伅锛屼粠鏈嶅姟绔洖浼犵殑娑堟伅浼氬湪event鐨刣ata灞炴€т腑銆?/p>

鎵嬪姩鍦ㄦ帶鍒跺彴璋冪敤send鍙戦€佹秷鎭紝鎵撳嵃event鍥炰紶淇℃伅:

鏈嶅姟绔畾鏃跺彂閫?/h4>

涓婇潰鏄粠瀹㈡埛绔彂閫佹秷鎭紝鏈嶅姟绔洖浼犮€傛垜浠篃鍙互閫氳繃setInterval璁╂湇鍔$鍦ㄥ浐瀹氭椂闂村彂閫佹秷鎭粰瀹㈡埛绔?

server.js淇敼濡備笅:

//褰揥ebSocket浠庡閮ㄨ繛鎺ユ椂鎵ц
wss.on(\'connection\',聽(ws)聽=>聽{
聽聽//杩炴帴鏃舵墽琛屾聽console聽鎻愮ず
聽聽console.log(\'Client聽connected\');
+聽聽//鍥哄畾鍙戦€佹渶鏂版秷鎭粰瀹㈡埛绔?+聽聽const聽sendNowTime聽=聽setInterval(()聽=>聽{
+聽聽聽聽ws.send(String(new聽Date()));
+聽聽},聽1000);
-聽聽//瀵筸essage璁剧疆鐩戝惉锛屾帴鏀朵粠瀹㈡埛绔彂閫佺殑娑堟伅
-聽聽ws.on(\'message\',聽(data)聽=>聽{
-聽聽聽聽//data涓哄鎴风鍙戦€佺殑娑堟伅锛屽皢娑堟伅鍘熷皝涓嶅姩杩斿洖鍥炲幓
-聽聽聽聽ws.send(data);
-聽聽});
聽聽//褰撀燱ebSocket鐨勮繛鎺ュ叧闂椂鎵ц
聽聽ws.on(\'close\',聽()聽=>聽{
聽聽聽聽console.log(\'Close聽connected\');
聽聽});
});
澶嶅埗浠g爜

瀹㈡埛绔繛鎺ュ悗灏变細瀹氭椂鎺ユ敹锛岀洿鑷虫垜浠叧闂瓀ebsocket鏈嶅姟

澶氫汉鑱婂ぉ

濡傛灉澶氫釜瀹㈡埛绔繛鎺ユ寜鐓т笂闈㈢殑鏂瑰紡鍙細杩斿洖鍚勮嚜鍙戦€佺殑娑堟伅锛屽厛娉ㄩ噴鏈嶅姟绔畾鏃跺彂閫侊紝寮€鍚袱涓獥鍙fā鎷燂細

濡傛灉鎴戜滑瑕佽瀹㈡埛绔棿娑堟伅鍏变韩锛屼篃鍚屾椂鎺ユ敹鍒版湇鍔$鍥炰紶鐨勬秷鎭憿锛?/p>

鎴戜滑鍙互浣跨敤clients鎵惧嚭褰撳墠鎵€鏈夎繛鎺ヤ腑鐨勫鎴风 锛屽苟閫氳繃鍥炰紶娑堟伅鍙戦€佸埌姣忎竴涓鎴风 涓細

淇敼server.js濡備笅:

...
//褰揥ebSocket浠庡閮ㄨ繛鎺ユ椂鎵ц
wss.on(\'connection\',聽(ws)聽=>聽{
聽聽//杩炴帴鏃舵墽琛屾聽console聽鎻愮ず
聽聽console.log(\'Client聽connected\');
-聽聽//鍥哄畾鍙戦€佹渶鏂版秷鎭粰瀹㈡埛绔?-聽聽const聽sendNowTime聽=聽setInterval(()聽=>聽{
-聽聽聽聽ws.send(String(new聽Date()));
-聽},聽1000);
+聽聽//瀵筸essage璁剧疆鐩戝惉锛屾帴鏀朵粠瀹㈡埛绔彂閫佺殑娑堟伅
+聽聽聽ws.on(\'message\',聽(data)聽=>聽{
+聽聽聽聽//鍙栧緱鎵€鏈夎繛鎺ヤ腑鐨劼犲鎴风
+聽聽聽聽let聽clients聽=聽wss.clients;
+聽聽聽聽//寰幆锛屽彂閫佹秷鎭嚦姣忎釜瀹㈡埛绔?+聽聽聽聽clients.forEach((client)聽=>聽{
+聽聽聽聽聽聽client.send(data);
+聽聽聽聽});
+聽聽聽});
聽聽//褰揥ebSocket鐨勮繛鎺ュ叧闂椂鎵ц
聽聽ws.on(\'close\',聽()聽=>聽{
聽聽聽聽console.log(\'Close聽connected\');
聽聽});
});
澶嶅埗浠g爜

杩欐牱涓€鏉ワ紝涓嶈鍦ㄥ摢涓鎴风鍙戦€佹秷鎭紝鏈嶅姟绔兘鑳藉皢娑堟伅鍥炰紶鍒版瘡涓鎴风 锛? 鍙互瑙傚療涓嬭繛鎺ヤ俊鎭?

鎬荤粨 馃

绾镐笂寰楁潵缁堣娴咃紝缁濈煡姝や簨瑕佽含琛?/strong>锛屽笇鏈涘ぇ瀹跺彲浠ユ妸鐞嗚閰嶅悎涓婇潰鐨勫疄渚嬭繘琛屾秷鍖栵紝鎼ソ鏈嶅姟绔篃鍙互鐩存帴浣跨敤娴嬭瘯宸ュ叿濂藉ソ鐜╄€嶄竴娉?/p>

鍙傝€冩枃绔?馃摐

鉂わ笍 闃竴宄?WebSocket 鏁欑▼

鉂わ笍 Using WebSockets on Heroku with Node.js

鉂わ笍 WebSocket 鏄粈涔堝師鐞嗭紵涓轰粈涔堝彲浠ュ疄鐜版寔涔呰繛鎺ワ紵

鎵╁睍 馃弳

濡傛灉浣犺寰楁湰鏂囧浣犳湁甯姪锛屽彲浠ユ煡鐪嬫垜鐨勫叾浠栨枃绔犫潳锔忥細

馃憤 Web寮€鍙戝簲浜嗚В鐨?绉嶈璁℃ā寮忦煃?/a>

馃憤 10涓畝鍗曠殑鎶€宸ц浣犵殑 vue.js 浠g爜鏇翠紭闆咅煃?/a>

馃憤 Web寮€鍙戝簲璇ョ煡閬撶殑鏁版嵁缁撴瀯馃崐

馃憤 缁忓吀闈㈣瘯棰橈紒浠庤緭鍏RL鍒伴〉闈㈠睍绀轰綘杩樹笉璧剁揣瀛﹁捣鏉ワ紵馃崐

馃憤 娴呰皥SSL鍗忚鐨勬彙鎵嬭繃绋嬸煃?/a>

以上是关于闆惰窛绂绘帴瑙ebsocket馃殌的主要内容,如果未能解决你的问题,请参考以下文章

POJ 1733锛堣竟甯︽潈骞舵煡闆?绂绘暎鍖栵級

[LeetCode] 244. Shortest Word Distance II 鏈€鐭崟璇嶈窛绂?II

java WebSocket 即时通讯服务端代码

WebSocket.之.基础入门-断开连接处理

webpack 3 姝e紡鍙戝竷锛岃浣犳洿鈥滃揩鈥?/h1>