• <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
            <2018年5月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789


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

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            相冊

            Awesome

            Blog

            Book

            GitHub

            Link

            搜索

            •  

            積分與排名

            • 積分 - 216841
            • 排名 - 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 思月行云 閱讀(2371) 評論(0)  編輯 收藏 引用 所屬分類: Nginx\Openresty
            尹人香蕉久久99天天拍| 久久97久久97精品免视看秋霞| 久久久久青草线蕉综合超碰| 国内高清久久久久久| 久久久精品免费国产四虎| 精品人妻伦九区久久AAA片69| 人人妻久久人人澡人人爽人人精品| 亚洲AV无码1区2区久久| 精品一久久香蕉国产线看播放| 伊人久久大香线蕉成人| 亚洲综合婷婷久久| 99精品久久精品一区二区| 国产亚洲精午夜久久久久久 | 久久99热国产这有精品| 久久久久亚洲AV无码专区网站| 日日噜噜夜夜狠狠久久丁香五月| 国产99久久久国产精免费| 久久久久久亚洲精品成人| 四虎影视久久久免费| 国产精品美女久久久| 久久久久亚洲AV成人网人人网站| 国产香蕉97碰碰久久人人| 国内精品伊人久久久久AV影院| 国产精品久久久久久久人人看| 国产福利电影一区二区三区久久老子无码午夜伦不 | 亚洲国产精品成人AV无码久久综合影院 | 婷婷久久综合| 久久久久久国产精品无码下载 | 久久r热这里有精品视频| A级毛片无码久久精品免费| 久久免费视频6| 国内精品免费久久影院| 国产精品久久久久久久久鸭| 久久久老熟女一区二区三区| 一本久久a久久精品亚洲| 麻豆av久久av盛宴av| 日韩电影久久久被窝网| 青青草原综合久久大伊人导航| 国产精品久久久久久久午夜片 | 久久久久免费精品国产| 国产成人综合久久精品红|