lua-resty-websocket icon indicating copy to clipboard operation
lua-resty-websocket copied to clipboard

how to access websocket clients / handles from websocket server?

Open gittyup2018 opened this issue 4 years ago • 0 comments

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?

gittyup2018 avatar Mar 11 '21 05:03 gittyup2018