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

sending messages to already created websocket objects

Open manish786 opened this issue 8 years ago • 2 comments

I have a list of websocket server objects that get created when client(s) call in on port A.

Another port, B listens for incoming http requests(no websockets here) on same server. The requirement is to send messages to websocket clients via already created websockets connections (on port A) based on requests on port B.

My initial hunch was just to test the created websocket object in different request contexts. To start with, I saved the websocket server object in a global table variable. Requests on port B seem to have access to earlier saved websocket object. Tried sending text using this object through it resulted in error:

lua entry thread aborted: runtime error: ...r/openresty/1.13.6.1/lualib/resty/websocket/protocol.lua:320: bad request
stack traceback:
coroutine 0:
	[C]: in function 'send'
	...r/openresty/1.13.6.1/lualib/resty/websocket/protocol.lua:320: in function '_send_frame'
	...lar/openresty/1.13.6.1/lualib/resty/websocket/server.lua:167: in function 'send_text'``

Any insights? I was going to try the ngx.shared.DICT next but wanted to check with you all. I am new to openresty and Lua

manish786 avatar Jan 05 '18 17:01 manish786

@manish786 You cannot directly cross the request boundary to manipulate a cosocket object that the current request does not create. The bad request error message clearly indicates this misuse.

This is a FAQ and has discussed for many times in the openresty mailing lists. Basically you pass data and control flow to the target request and let the target request manipulate its own cosocket. You can use worker-level Lua tables, queues based on lua_shared_dict, and/or ngx.semaphore to achieve this.

agentzh avatar Jan 05 '18 19:01 agentzh

@agentzh

I attempt to convert many requests (tcp raw stream request) on different connections to one websocket-connection,but I got 'bad request' error message. I try to new a websocket-client in content_by_lua_file stage ,it will cause 'bad request' error message. If I use ngx.semaphore ,which stage what new a websocket-client and connect to server is right ?

JimmyBaize avatar Jan 23 '20 08:01 JimmyBaize