• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            Fork me on GitHub
            隨筆 - 215  文章 - 13  trackbacks - 0
            <2017年5月>
            30123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910


            專注即時通訊及網游服務端編程
            ------------------------------------
            Openresty 官方模塊
            Openresty 標準模塊(Opm)
            Openresty 三方模塊
            ------------------------------------
            本博收藏大部分文章為轉載,并在文章開頭給出了原文出處,如有再轉,敬請保留相關信息,這是大家對原創作者勞動成果的自覺尊重!!如為您帶來不便,請于本博下留言,謝謝配合。

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            相冊

            Awesome

            Blog

            Book

            GitHub

            Link

            搜索

            •  

            積分與排名

            • 積分 - 215465
            • 排名 - 118

            最新評論

            閱讀排行榜

            https://www.cnblogs.com/scotoma/p/3330190.html

            作者:楊鑫奇

            關注Openresty很久了,期待支持websocket終于出來了,看到Aapo Talvensaari同學寫的文章https://medium.com/p/1778601c9e05,興奮下也來測試下,之前用websocket做即時通訊,還是基于socket.io的例子,現在用nginx來做...初嘗試下,竟然報錯了,章哥的解答在這里:
            https://github.com/agentzh/lua-resty-websocket/issues/2 ,現在配置成功了,將自己的配置過程寫下來,希望對大家有所幫助.

            主要原因是:

            websocket依賴于 lua-nginx-module,得用最新版本的,下面是章哥給的配置.
            我用最新的1.4.2.7編譯并測試成功的了.

            到自己的目錄下:
            下載最新版本的 openresty 和 lua-nginx-module 然后安裝:
            wget http://openresty.org/download/ngx_openresty-1.4.2.7.tar.gz
            tar zxvf ngx_openresty-1.4.2.7.tar.gz
            git clone https://github.com/chaoslawful/lua-nginx-module.git
            cd lua-nginx-module
            git checkout -b websocket origin/websocket
            cd ../ngx_openresty-1.4.2.7/bundle
            rm -Rf ngx_lua-0.8.9
            ln -s ../../lua-nginx-module ngx_lua-0.8.9
            cd ..
            ./configure -with-luajit -prefix=/usr/local 
            gmake && gmake install

            安裝完成后
            cd ../
            git clone https://github.com/agentzh/lua-resty-websocket.git
            拷貝websocket到lualib目錄下
            cp -r lua-resty-websocket/lib/resty/websocket /usr/local/lualib/resty/

            配置自己的nginx conf的內容
            在nginx.conf中添加lualib的路徑
            lua_package_path "/usr/local/lualib/resty/websocket/?.lua;;";

            我這里是獨立開的yagamiko.conf,添加websocket:
            在server段內,修改添加以下內容:

            listen 80 default so_keepalive=2s:2s:8;

            這個是Aapo Talvensaari同學寫的測試代碼: 

            location /1.0/websocket {

              lua_socket_log_errors off;

              lua_check_client_abort on;

              content_by_lua '

                local server = require "resty.websocket.server"

                local wb, err = server:new{

                timeout = 5000,  -- in milliseconds

                max_payload_len = 65535,

                }

                if not wb then

                  ngx.log(ngx.ERR, "failed to new websocket: ", err)

                  return ngx.exit(444)

                end

                while true do

                  local data, typ, err = wb:recv_frame()

                  if wb.fatal then

                    ngx.log(ngx.ERR, "failed to receive frame: ", err)

                    return ngx.exit(444)

                  end

                  if not data then

                    local bytes, err = wb:send_ping()

                    if not bytes then

                      ngx.log(ngx.ERR, "failed to send ping: ", err)

                      return ngx.exit(444)

                    end

                  elseif typ == "close" then break

                  elseif typ == "ping" then

                    local bytes, err = wb:send_pong()

                    if not bytes then

                      ngx.log(ngx.ERR, "failed to send pong: ", err)

                      return ngx.exit(444)

                    end

                  elseif typ == "pong" then

                    ngx.log(ngx.INFO, "client ponged")

                  elseif typ == "text" then

                    local bytes, err = wb:send_text(data)

                    if not bytes then

                      ngx.log(ngx.ERR, "failed to send text: ", err)

                      return ngx.exit(444)

                    end

                  end

                end

                wb:send_close()

              ';

            }

            然后重新啟動nginx就可以了...

            使用 這個哥們提到的測試html,就可以了 https://medium.com/p/1778601c9e05

            <html>
            <head>
            <script>
            var ws = null;
            function connect() {
              
            if (ws !== nullreturn log('already connected');
              ws 
            = new WebSocket('ws://ko.local.freeflare.com/1.0/websocket');
              ws.onopen = function () {
                log('connected');
              };
              ws.onerror 
            = function (error) {
                log(error);
              };
              ws.onmessage 
            = function (e) {
                log('recv: ' 
            + e.data);
              };
              ws.onclose 
            = function () {
                log('disconnected');
                ws 
            = null;
              };
              
            return false;
            }
            function disconnect() {
              
            if (ws === nullreturn log('already disconnected');
              ws.close();
              
            return false;
            }
            function send() {
              
            if (ws === nullreturn log('please connect first');
              
            var text = document.getElementById('text').value;
              document.getElementById('text').value 
            = "";
              log('send: ' 
            + text);
              ws.send(text);
              
            return false;
            }
            function log(text) {
              
            var li = document.createElement('li');
              li.appendChild(document.createTextNode(text));
              document.getElementById('log').appendChild(li);
              
            return false;
            }
            </script>
            </head>
            <body>
              <form onsubmit="return send();">
                <button type="button" onclick="return connect();">
                  Connect
                </button>
                <button type="button" onclick="return disconnect();">
                  Disconnect
                </button>
                <input id="text" type="text">
                <button type="submit">Send</button>
              </form>
              <ol id="log"></ol>
            </body>
            </html>

            測試....這里注意在同一個域名下就好了....

            測試成功了....


            posted on 2018-05-04 11:15 思月行云 閱讀(2348) 評論(0)  編輯 收藏 引用 所屬分類: Nginx\Openresty
            aaa级精品久久久国产片| 久久人做人爽一区二区三区| 国内精品久久人妻互换| 久久国产精品99久久久久久老狼| 精品久久久久久亚洲精品| 超级碰久久免费公开视频| 久久伊人精品青青草原日本| 麻豆精品久久久久久久99蜜桃 | 久久国产精品无码一区二区三区| 国内精品久久久久影院优 | 久久久精品日本一区二区三区| 亚洲一级Av无码毛片久久精品| 精品国产乱码久久久久软件| 久久成人国产精品二三区| 中文成人无码精品久久久不卡| 精品久久久久久| 亚洲乱码精品久久久久..| 国产无套内射久久久国产| 综合久久国产九一剧情麻豆 | 亚洲AV无一区二区三区久久| 久久久精品人妻无码专区不卡| 99精品久久精品一区二区| 久久中文字幕人妻熟av女| 99热精品久久只有精品| 久久国产高潮流白浆免费观看| 精品伊人久久久| 久久久久国产一区二区三区| 国产午夜精品久久久久免费视| 精品久久亚洲中文无码| 久久婷婷午色综合夜啪| 亚洲精品无码久久久久久| 91久久精品视频| 国产精品九九九久久九九| 久久国产色AV免费看| 久久ww精品w免费人成| 一本色道久久HEZYO无码| 久久久SS麻豆欧美国产日韩| 久久无码专区国产精品发布 | 国产午夜久久影院| 国产精品久久国产精品99盘| 2020久久精品国产免费|