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

send message from server to specific user

Open ovoshlook opened this issue 8 years ago • 4 comments

Hi is websocket have any way to send message from server to specific user Like ws:send_text("some text",receiver_ip)?

ovoshlook avatar Jun 01 '16 12:06 ovoshlook

Each web socket connection is for specific user. And your server has these handles. You could set up a queue based on say REMOTE_ADDR etc. But I would use something that is unique to user. More than one user can share the same IP behind the NAT.

bungle avatar Jun 01 '16 12:06 bungle

Thanks for reply. So am i right that i can get somewhere source IP of each user that connecting to websocket server like ngx.source_ip() or something like this?

ovoshlook avatar Jun 01 '16 13:06 ovoshlook

@ovoshlook, of course, you may grap it from a normal nginx variable ngx.var.remote_addr, but I wouldn't use it. You should at least check for reverse proxies, e.g.: https://github.com/bungle/lua-resty-session/blob/master/lib/resty/session.lua#L235-L252 (session module btw. doesn't check address by default because of many problems it might cause - but in a controlled environment, it is okay).

bungle avatar Jun 01 '16 21:06 bungle

使用ngx.semaphore可以完成对应功能

ouyangruoxue avatar Mar 12 '17 12:03 ouyangruoxue