Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sending messages to already created websocket objects #34

Open
manish786 opened this issue Jan 5, 2018 · 2 comments
Open

sending messages to already created websocket objects #34

manish786 opened this issue Jan 5, 2018 · 2 comments

Comments

@manish786
Copy link

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

@agentzh
Copy link
Member

agentzh commented Jan 5, 2018

@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.

@JimmyBaize
Copy link

@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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants