ÔõÑùʹÓÃNginx Proxy ManagerʵÏÖWebsocketsÊðÀí
ÔõÑùʹÓÃNginx Proxy ManagerʵÏÖWebsocketsÊðÀí
WebsocketsÊÇÒ»ÖÖʵʱͨѶÐÒ飬ÊÊÓÃÓÚÐèҪ˫ÏòͨѶµÄÓ¦ÓóÌÐò¡£¶øNginx Proxy Manager£¨¼ò³ÆNPM£©ÊÇÒ»¸ö»ùÓÚNginxµÄÊðÀíЧÀÍÆ÷£¬¿ÉÒÔÓÃÀ´ÖÎÀíºÍÉèÖöà¸ö·´ÏòÊðÀíµÄ×ÊÔ´¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃNPMÀ´ÊµÏÖWebsocketsÊðÀí£¬²¢ÌṩÏêϸµÄ´úÂëʾÀý¡£
×°ÖÃNPM
Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖÃNPM¡£ÔÚUbuntuϵͳÉÏ£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî×°Öãº
sudo apt-get update sudo apt-get install npm
µÇ¼ºó¸´ÖÆ
ÉèÖÃNPM
×°ÖÃÍêNPMºó£¬ÎÒÃÇÐèÒª¾ÙÐÐһЩÉèÖá£Ê×ÏÈ£¬½øÈëNPMµÄ×°ÖÃĿ¼£¬Ò»Ñùƽ³£ÊÇ/usr/share/nginx/html£¬È»ºó½¨ÉèÒ»¸öÃûΪ config.jsonµÄÎļþ£¬ÓÃÀ´ÉèÖÃÊðÀíЧÀÍÆ÷¡£
ÔÚ config.json ÎļþÖУ¬ÎÒÃÇ¿ÉÒÔÉèÖöà¸öÊðÀíЧÀÍÆ÷¡£ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇÉèÖÃÒ»¸öÃûΪ websocket µÄÊðÀíЧÀÍÆ÷£¬½«ËùÓÐÎüÊÕµ½µÄWebsocketsÇëÇóÊðÀíµ½Ö¸¶¨µÄÄ¿µÄЧÀÍÆ÷¡£
ÏÂÃæÊÇÒ»¸öÉèÖõÄÀý×Ó£º
{ "proxies": { "websocket": { "name": "Websockets Proxy", "ssl": false, "host": "ws://localhost:8000", "port": 80, "path": "/websocket", "proxyType": "websocket" } } }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖУ¬ÎÒÃÇÖ¸¶¨ÁËÊðÀíЧÀÍÆ÷µÄÃû³Æ¡¢ÊÇ·ñʹÓÃSSL¡¢Ä¿µÄЧÀÍÆ÷µÄÖ÷»úºÍ¶Ë¿Ú¡¢URL·¾¶ÒÔ¼°ÊðÀíÀàÐÍ¡£
Æô¶¯NPM
ÉèÖÃÍê³Éºó£¬ÎÒÃǾͿÉÒÔÆô¶¯NPMÁË¡£ÔÚÖÕ¶ËÖнøÈëNPMµÄ×°ÖÃĿ¼£¬È»ºóÔËÐÐÈçÏÂÏÂÁ
sudo npm start
µÇ¼ºó¸´ÖÆ
´Ëʱ£¬NPM»á¼àÌýÔÚĬÈϵÄ80¶Ë¿ÚÉÏ£¬×îÏÈÊðÀíÇëÇó¡£
²âÊÔWebsocketsÊðÀí
ÏÖÔÚÎÒÃÇÒѾÍê³ÉÁËNPMµÄÉèÖúÍÆô¶¯£¬½ÓÏÂÀ´²âÊÔÒ»ÏÂÎÒÃǵÄWebsocketsÊðÀíÊÇ·ñÕý³£ÊÂÇé¡£
Ê×ÏÈ£¬×¼±¸Ò»¸ö¼òÆÓµÄWebsocketsЧÀÍÆ÷£¬¿ÉÒÔʹÓÃNode.js¾ÙÐд¡£ÏÂÃæÊÇÒ»¸öʾÀý´úÂ룺
const WebSocket = require('ws'); const wss = new WebSocket.Server({ port: 8000 }); wss.on('connection', ws => { ws.on('message', message => { console.log(`Received message: ${message}`); ws.send(`Echo: ${message}`); }); ws.send('Connected to server.'); });
µÇ¼ºó¸´ÖÆ
ÔÚÖÕ¶ËÖÐÔËÐÐÉÏÊö´úÂ룬Æô¶¯WebsocketsЧÀÍÆ÷¡£
½ÓÏÂÀ´£¬ÔÚä¯ÀÀÆ÷Öлá¼ûNPMµÄÖÎÀí½çÃ棬ͨ³£ÊÇ http://localhost¡£ÔÚ½çÃæÉÏ£¬µã»÷ Add Proxy Host °´Å¥£¬ÌîдÈçÏÂÐÅÏ¢£º
Proxy Hostname: localhost
Proxy Port: 80
Proxy Protocol: http
Proxy Type: websocket
Proxy Destination: localhost:8000
µã»÷ Save °´Å¥ÉúÑÄÉèÖá£
ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔʹÓÃí§ÒâÖ§³ÖWebsocketsµÄ¿Í»§¶ËÅþÁ¬µ½ ws://localhost/websocket£¬·¢ËÍÐÂÎŲ¢ÎüÊÕЧÀÍÆ÷µÄ»Ø¸´¡£
ͨ¹ýNPMÊðÀíЧÀÍÆ÷£¬ÎÒÃÇÀÖ³ÉʵÏÖÁËWebsocketsµÄÊðÀí¹¦Ð§¡£
×ܽá
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃNginx Proxy ManagerÀ´ÊµÏÖWebsocketsµÄÊðÀí¹¦Ð§¡£ÎÒÃÇͨ¹ýÉèÖÃNPM²¢Ê¹ÓÃÏêϸµÄ´úÂëʾÀý£¬ÀֳɴÁËÒ»¸öÄܹ»ÊðÀíWebsocketsÇëÇóµÄÊðÀíЧÀÍÆ÷¡£Ï£Íû±¾ÎĶÔÄãÃ÷È·ºÍʹÓÃNPMÒÔ¼°WebsocketsÊðÀíÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃNginx Proxy ManagerʵÏÖWebsocketsÊðÀíµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡