Skip to content

how to access websocket clients / handles from websocket server? #60

@gittyup2018

Description

@gittyup2018

I am new to using websockets with openresty so I apologize if it is documented somewhere but I can't find it and have been trying for several hours unsuccessfully.

is there a way to get and loop through the websocket server handles for each client?

When a new client connects to the server, is there a way to store the socket handle to send a message directly to that socket, or disconnect the socket, etc? I thought I could just store the object in a shared dict but that is for text only and it fails when trying to json encode it:

local json=require("cjson.safe");
local cache=ngx.shared.cache;
local server = require "resty.websocket.server"
local wb, err = server:new{
timeout = 5000,
max_payload_len = 65535
}

local ok,err=json.encode(wb);
if not ok then trace("JSON ERR> " , err);
end;

trace(): JSON ERR> ,*5 lua entry thread aborted: runtime error: /usr/local/openresty/lualib/resty/core/shdict.lua:186: attempt to compare string with number

So how can I save/store the websocket client handle to access it directly?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions