OpenResty作为WebSocket服务器如果想要支持wss://协议应该如何配置?

276 views
Skip to first unread message

u2013...@hust.edu.cn

unread,
Sep 5, 2017, 6:53:49 AM9/5/17
to openresty
Hi,

OpenResty提供了作为WebSocket服务器支持ws://协议的配置方法:

ngx.conf:
#########################################################
user root;                              # Running in root
worker_processes  auto;      # Defines the number of worker processes.
                                              # Setting it to auto autodetects the num of CPU core numbers
error_log  logs/error.log  info;
events {
    worker_connections  512;   # Sets the maximum number of simultaneous connections
                                                # that can be opened by a worker process.
}

http {
    include       mime.types;                            # necessary
    default_type  application/octet-stream;     # Defines the default MIME type of a response.
    lua_package_path '/usr/local/openresty/lualib/?.lua;;';
    sendfile        on;                                         # To applications except for high IOPS, it must be set to on,
                                                                       # otherwise the pictures may cannot be showed
    keepalive_timeout  75s;                             # the keep-alive client connection will stay open on the serverside during 75s

    server {
        listen 80;                           # port 80
        server_name websocket_server;
        lua_code_cache off;

        location /websocket_server{
            content_by_lua_file lua/websocket_server.lua;
        }
    }
}
#########################################################
websocket_server.lua: 与github上的相同
#########################################################
  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

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

    if not data then
        ngx.log(ngx.ERR, "failed to receive a frame: ", err)
        return ngx.exit(444)
    end

    if typ == "close" then
        -- send a close frame back:

        local bytes, err = wb:send_close(1000, "enough, enough!")
        if not bytes then
            ngx.log(ngx.ERR, "failed to send the close frame: ", err)
            return
        end
        local code = err
        ngx.log(ngx.INFO, "closing with status code ", code, " and message ", data)
        return
    end

    if typ == "ping" then
        -- send a pong frame back:

        local bytes, err = wb:send_pong(data)
        if not bytes then
            ngx.log(ngx.ERR, "failed to send frame: ", err)
            return
        end
    elseif typ == "pong" then
        -- just discard the incoming pong frame

    else
        ngx.log(ngx.INFO, "received a frame of type ", typ, " and payload ", data)
    end

    wb:set_timeout(1000)  -- change the network timeout to 1 second

    bytes, err = wb:send_text("Hello world")
    if not bytes then
        ngx.log(ngx.ERR, "failed to send a text frame: ", err)
        return ngx.exit(444)
    end

    bytes, err = wb:send_binary("blah blah blah...")
    if not bytes then
        ngx.log(ngx.ERR, "failed to send a binary frame: ", err)
        return ngx.exit(444)
    end

    local bytes, err = wb:send_close(1000, "enough, enough!")
    if not bytes then
        ngx.log(ngx.ERR, "failed to send the close frame: ", err)
        return
    end
#########################################################
但是如果想让WebSocket服务器支持wss://协议应该如何配置呢?应该做哪些改动?

Thanks & Best Regards,
Peng


rongfeng liang

unread,
Sep 5, 2017, 8:03:58 PM9/5/17
to openresty
你加上证书 走 https 就可以了

在 2017年9月5日星期二 UTC+8下午6:53:49,u2013...@hust.edu.cn写道:
Reply all
Reply to author
Forward
0 new messages