• pushpin Server-sent events && openresty 集成试用


    前边有写过一个简单pushpin 集成stream 的demo,这次测试下sse 的功能
    备注: 环境依然使用的是docker-compose运行

    环境准备

    • docker-compose 文件
    version: "3"
    services:
      pushpin:
        image: fanout/pushpin
        environment:
        - "target=api:8080"
        - "LOGNAME=nobody"
        volumes:
        - "./routes:/etc/pushpin/routes"
        ports:
        - "5561:5561"
        - "5562:5562"
        - "5563:5563"
        - "7999:7999"
      api:
        build: ./
        ports:
        - "8080:8080"
        volumes:
        - "./nginx_lua/:/opt/app/"
        - "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
    • nginx.conf
    worker_processes 1;
    user root;  
    events {
        worker_connections 1024;
    }
    http {
        include mime.types;
        default_type application/octet-stream;
        sendfile on;
        lua_code_cache off;
        lua_need_request_body on;
        gzip on;
        resolver 127.0.0.11 ipv6=off;          
        real_ip_header X-Forwarded-For;
        real_ip_recursive on;
        lua_package_path '/opt/app/?.lua;;';
        server {
            listen 8080;
            server_name app;
            charset utf-8;
            default_type text/html;
            location / {
               default_type text/plain;
               index index.html index.htm;
            }
            location = /favicon.ico {
                root /opt/app/static;
            }
           # sse 测试
            location /sse {
                 content_by_lua_block {
                  require("api/sse")()
                }
            }
           ## stream 测试
            location /userinfo {
                content_by_lua_block {
                  require("api/userinfo")()
                }
            }
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {
                root html;
            }
    
        }
    }
    • dockerfile
    FROM openresty/openresty:alpine-fat
    LABEL author="1141591465@qq.com"
    RUN /usr/local/openresty/luajit/bin/luarocks install lua-resty-http
    RUN opm get thibaultcha/lua-resty-jit-uuid

    sse lua 代码

    sse 代码和stream 类似,主要是content_type 以及content 内容格式
    nginx_lua/api/sse.lua

    local json = require("cjson")
    local http = require("resty.http")
    function init()
        local uuid = require("resty.jit-uuid")
        uuid.seed()
        local method_name = ngx.req.get_method()
        if method_name == "GET" then
            ngx.header["Content-Type"] = "text/event-stream"
            ngx.header["Grip-Hold"] = "stream"
            ngx.header["Grip-Channel"] = "loginfo"
            ngx.say("is stream open")
            return
        end
        ngx.req.read_body()
        local body = ngx.req.get_body_data()
        if not body then
            if ngx.req.get_body_file() then
                return nil, "request body did not fit into client body buffer, consider raising 'client_body_buffer_size'"
            else
                return ""
            end
        end
        local mode = json.decode(body)
        -- event: update
    data: hello world
    
    
    
        local body_entity = {
            items = {
                {
                    channel = mode.channel,
                    id = uuid(),
                    formats = {
                        ["http-stream"] = {
                            content = mode.data,
                        },
                        -- action = "send"
                    }
                }
            }
        }
        ngx.log(ngx.ERR, json.encode(body_entity))
        local requestapiurl = "http://pushpin:5561/publish/"
        local httpc = http.new()
        local res, err =
            httpc:request_uri(
            requestapiurl,
            {
                method = "POST",
                body = json.encode(body_entity),
                headers = {
                    ["Content-Type"] = "application/json"
                }
            }
        )
        if not res then
            ngx.say("failed to request: ", err)
            return
        end
        ngx.log(ngx.ERR, res.body)
        ngx.say(res.body)
    end
    return init

    启动&&测试

    • 启动
    docker-compose up -d
    curl -X POST 
      http://localhost:8080/sse 
      -H 'Content-Type: application/json' 
      -H 'Postman-Token: 0c28ab8b-1128-47cf-92b5-8cf6061dbff7' 
      -H 'cache-control: no-cache' 
      -d '{
     "channel":"loginfo",
     "data": "event: userlogin
    data: demo login 
    
    "
    }'

    效果

    说明

    pushpin 的功能还是很强大的,开发起来也比较简单,对于我们需要realtime api 的需求还是很方便的

    参考资料

    https://pushpin.org/docs/usage/#subscribing
    https://github.com/rongfengliang/pushpin-openresty-docker-compose

  • 相关阅读:
    Diffbot:开发者工具 将web内容转换成应用
    算法之道—形而之上谓之道
    css三个ppt
    SMB的NTLM认证过程与NTLM挑战的编程实现
    c++计算圆周率
    SVN总结
    struts2技术实现用户名唯一的验证处理详解
    数字常量
    二叉树的创建和遍历
    php变量的定义和作用域
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/10354990.html
Copyright © 2020-2023  润新知