[急]求助!求助!求助!求助!求助!求助!

130 views
Skip to first unread message

6 Mr.Tian172

unread,
Dec 25, 2018, 11:45:01 AM12/25/18
to open...@googlegroups.com
最近在做服务器通信这一块东西,对于openresty也是刚刚接触,正好在网上了解到lua_resty_websocket这个强大的神器,所以就迫不及待的试了一下。
我本意是要实现客户端与服务器实现全双工通信,服务器可以主动向客户端推送消息(单播,广播)
我的思路:
       服务器上部署websocket.server,客户端通过发送wss协议去想服务器握手完成订阅。
       服务器将过来的连接存储到一个外表中(保持连接的ws),如果断开连接超出可我预设的时间,服务器主动把这个链接从外表中删除。服务器从外表中遍历链接,(启用多个接收线程和一个推送线程)每个链接启用一个轻线程去无间断运行,从而保证能即时的接受来自客户端的消息,存到我预设的shared中进行保存从而达到进程间共享数据的目的,每个数据都是以kv去存的,k为连接的唯一id(自己设置的uuid,够用了);我配了一个服务器用来存储的shared用来存储从服务器推送的数据,里面存的是json字符串,每个字符串中有连个kv,第一个为data(也就是我想推送的数据),第二个为user(就是我想推送给哪个连接),在服务器发http请求会携带这两个kv到推送线程,推送线程会解析json,分出data 和 user 在根据user中的value(也就是每个连接的唯一id)从外表中查找连接,然后进行send_text(),完成推送。单客户端推送没问题,就是多个的时候会出现下面的问题:
    
image.png
看了看server源码,估计是少了什么参数或者思路问题,困扰了很久,虽然对于各位大佬这不是什么大的问题,但是作为一个年轻的程序员来说,可谓是食不下咽呢!还请大家帮忙解惑!谢谢!

下面是我从服务器推送数据的步骤:
image.png
image.png
image.png
image.png
image.png
image.png
image.png

--
  田晓勇
 上海 

tokers

unread,
Dec 25, 2018, 9:15:59 PM12/25/18
to openresty
Hello!

报错信息已经解释了原因了。

lua-resty-websocket 本质上使用的还是 Cosocket。
Cosocket 不能够跨请求使用,其内部涉及一些请求相关的内存分配。
这一点在文档里已经提及:

The cosocket object created by this API function has exactly the same lifetime as the Lua handler creating it. So never pass the cosocket object to any other Lua handler (including ngx.timer callback functions) and never share the cosocket object between different NGINX requests.


Appla

unread,
Dec 25, 2018, 9:25:04 PM12/25/18
to openresty
 
The cosocket object created by this API function has exactly the same lifetime as the Lua handler creating it. So never pass the cosocket object to any other Lua handler (including ngx.timer callback functions) and never share the cosocket object between different NGINX requests.
 
 ngx.socket.tcpngx.socket.udp, and ngx.req.socket these objects' lifetime is bound to the request context creating them while the timer callback is detached from the creating request's context (by design) and runs in its own (fake) request context. If you try to share the thread or cosocket objects across the boundary of the creating request, then you will get the "no co ctx found" error (for threads) or "bad request" (for cosockets). It is fine, however, to create all these objects inside your timer callback. 

tianxia...@gmail.com

unread,
Dec 25, 2018, 9:36:42 PM12/25/18
to openresty
Hello

现在看来是我本身的思路都是有问题啊。这就比较悲催了!不知道您有没有什么好的思路借鉴一下呀?
在 2018年12月26日星期三 UTC+8上午10:15:59,tokers写道:

tianxia...@gmail.com

unread,
Dec 25, 2018, 9:38:19 PM12/25/18
to openresty
Hello  
谢谢您的回复,现在已经知道了报错的原因,不知道您有没有好的思路借鉴一下,我真的是绞尽脑汁了...

在 2018年12月26日星期三 UTC+8上午10:25:04,Appla写道:

Nick Xiao

unread,
Dec 27, 2018, 8:21:48 PM12/27/18
to open...@googlegroups.com
openresty 做推送已经很成熟了. 

dejiang 同学几年前就做过, 里面有一个架构图. 你可以看看:


另外, 你的邮件 subject 是典型的反面教材. subject 应该高度概括问题. 加再多的 "求助" 并没有用.

./nickr

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html

Reply all
Reply to author
Forward
0 new messages