尊龙凯时人生就是博

ÔõÑùʹÓÃNginxʵÏÖWebSocketЭÒéÖ§³Ö

ÔõÑùʹÓÃnginxʵÏÖwebsocketЭÒéÖ§³Ö

WebSocketЭÒéÊÇÒ»ÖÖÔÚWebÓ¦ÓóÌÐòÖÐʵÏÖË«ÏòͨѶµÄЭÒé £¬ËüÔÊÐíЧÀÍÆ÷×Ô¶¯·¢ËÍÊý¾Ýµ½¿Í»§¶Ë £¬¶ø²»ÐèÒª¿Í»§¶ËÊ×ÏÈÌᳫÇëÇó¡£Ïà±ÈÓڹŰåµÄHTTPЭÒé £¬WebSocketЭÒé¾ßÓиüµÍµÄÑӳٺ͸ü¸ßµÄЧÂÊ £¬ÊÊÓÃÓÚʵʱÐÔÒªÇó½Ï¸ßµÄÓ¦Óó¡¾°¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃNginx×÷Ϊ·´ÏòÊðÀíÀ´Ö§³ÖWebSocketЭÒé¡£

NginxÊÇÒ»¸ö¸ßÐÔÄܵĿªÔ´·´ÏòÊðÀíЧÀÍÆ÷ £¬¿ÉÒÔÓÃÓÚ¸ºÔØƽºâ¡¢·´ÏòÊðÀí¡¢¾²Ì¬Îļþ»º´æµÈ¶àÖÖ³¡¾°¡£NginxÒ²ÌṩÁËÒ»Ð©Ä  £¿éºÍÖ¸ÁîÀ´Ö§³ÖWebSocketЭÒé¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄÉèÖÃʾÀý£º

http {
    # ÆäËûµÄhttpÉèÖÃ

    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    server {
        listen 80;

        location /ws/ {
            proxy_pass http://backend;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÊöÉèÖÃÖÐ £¬ÎÒÃǽç˵ÁËÒ»¸ö/ws/·¾¶ £¬ÓÃÓÚ´¦ÀíWebSocketµÄÅþÁ¬ÇëÇó¡£WebSocketµÄÇëÇ󽫻ᱻÊðÀíµ½http://backendµØµãÉÏ¡£proxy_passÖ¸ÁîÓÃÓÚÉèÖÃÊðÀíµÄºó¶ËЧÀÍÆ÷µØµã £¬proxy_http_versionÖ¸ÁîÓÃÓÚÉèÖÃÊðÀíµÄHTTPЭÒé°æ±¾¡£proxy_set_headerÖ¸ÁîÓÃÓÚÉèÖÃÇëÇóÍ·ÐÅÏ¢ £¬ÆäÖÐUpgradeºÍConnectionÊDZØÐèµÄ £¬ÓÃÓÚ¼û¸æЧÀÍÆ÷¾ÙÐÐЭÒéÉý¼¶¡£

ÐèҪעÖصÄÊÇ £¬ÉÏÊöÉèÖÃÖеÄmapÖ¸ÁîÓÃÓÚ½«¿Í»§¶ËÇëÇóÍ·ÖеÄUpgrade×Ö¶ÎÓ³ÉäΪ$http_upgrade±äÁ¿ £¬²¢Æ¾Ö¤ÆäÖµÀ´¶¯Ì¬ÉèÖÃ$connection_upgrade±äÁ¿¡£ÕâÑù¿ÉÒÔʵÏÖÔÚÇëÇóÖз¢Ã÷Upgrade×Ö¶Îʱ £¬½«Upgrade×ֶεÄÖµÉèÖÃΪ$connection_upgrade×ֶεÄÖµ £¬²»È»½«¹Ø±ÕÅþÁ¬¡£

ÔÚÉèÖÃÍê³Éºó £¬ÎÒÃÇÖ»ÐèÒª½«NginxÆô¶¯¼´¿É£º

sudo service nginx start

µÇ¼ºó¸´ÖÆ

ÏÖÔÚ £¬ÎÒÃÇÒѾ­Íê³ÉÁËʹÓÃNginx×÷Ϊ·´ÏòÊðÀíÀ´Ö§³ÖWebSocketЭÒéµÄÉèÖá£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëƬ¶ÏÀ´²âÊÔWebSocketµÄÅþÁ¬£º

var socket = new WebSocket("ws://yourdomain.com/ws/");
socket.onopen = function () {
    console.log("Connection established.");
};
socket.onmessage = function (event) {
    console.log("Received message: ", event.data);
};
socket.onclose = function () {
    console.log("Connection closed.");
};

µÇ¼ºó¸´ÖÆ

½«ws://yourdomain.com/ws/Ì滻ΪÏÖʵµÄWebSocketµØµã £¬²¢·­¿ªä¯ÀÀÆ÷µÄ¿ª·¢Õß¹¤¾ßÉó²é¿ØÖÆ̨Êä³ö¡£ÈôÊÇÄܹ»Õý³£ÅþÁ¬²¢ÊÕµ½ÐÂÎÅ £¬ÄÇô˵Ã÷WebSocketЭÒéÒѾ­Àֳɵر»NginxÖ§³Ö¡£

×ÛÉÏËùÊö £¬Í¨¹ýÒÔÉÏÉèÖúʹúÂëʾÀý £¬ÎÒÃÇ¿ÉÒÔÀû±ãµØʹÓÃNginxÀ´Ö§³ÖWebSocketЭÒé £¬´Ó¶øʵÏÖʵʱÐÔÒªÇó½Ï¸ßµÄË«ÏòͨѶ¡£

ÒÔÉϾÍÊÇÔõÑùʹÓÃNginxʵÏÖWebSocketЭÒéÖ§³ÖµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是博ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是博ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是博

13452372176

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ