在Cowboy中的HTTP处理程序和websocket处理程序之间进行通信 [英] communicating between http handler and websocket handler in Cowboy
问题描述
我想在Cowboy中创建一个websocket应用,该应用从另一个Cowboy处理程序获取其数据。假设我要结合牛仔的Echo_get示例: https:// github.com/ninenines/cowboy/tree/master/examples/echo_get/src
I'd like to create a websocket app in Cowboy that gets its data from another Cowboy handler. Let's say that I want to combine the Echo_get example from cowboy: https://github.com/ninenines/cowboy/tree/master/examples/echo_get/src
带有websocket示例
with websocket example
https://github.com/ninenines/cowboy/tree / master / examples / websocket / src
这样,对Echo的GET请求应通过websocket处理程序向html页面发送推送在那个例子中。
in such a way that a GET request to Echo should send a "push" via websocket handler to the html page in that example.
最简单的方法是什么?我可以通过一些简单的方法使用 pipe运算符吗?我是否需要创建和中间gen_something在它们之间传递消息?我希望得到一个示例代码的答案,该代码显示了处理程序的粘合代码-我真的不知道从哪里开始将两个处理程序连接在一起。
What is the least complicated way to do it? Can I use "pipe" operator in some easy way ? Do I need to create and intermediate gen_something to pass messages between them? I would appreciate an answer with sample code that shows the glue code for the handlers - I do not really know where to start plumbing the two handlers together.
推荐答案
牛仔的Websocket处理程序是一个长期存在的请求过程,您可以向其发送Websocket或erlang消息。
A websocket handler in cowboy is a long lived request process to which you can send websocket or erlang messages.
在您的情况下,有两种类型进程数:
In your case, there are 2 types of processes:
- websocket进程:向客户端打开了具有websocket连接的websocket处理程序。
- echo进程:当客户端使用参数访问回显处理程序时进行的处理。
其思想是,回显进程将erlang消息发送给
The idea is that the echo process sends an erlang message to the websocket processes, that in turn will send a message to the client.
为使回显进程可以向websocket进程发送消息,您需要保留一个websocket列表。您要向其发送消息的进程。 Gproc 是用于此目的的非常有用的库。
For your echo process can send a message to your websocket processes, you need to keep a list of websocket processes to which you want to send messages. Gproc is a quite useful library for that purpose.
您可以使用<$ c将进程注册到 gproc pubsub $ c> gproc_ps:subscribe / 2 ,然后使用 gproc_ps:publish / 3
将消息发送到已注册的进程。
You can register processes to gproc pubsub with gproc_ps:subscribe/2
, and send messages to the registered processes with gproc_ps:publish/3
.
Cowboy Websocket进程使用 websocket_info / 3 函数。
Cowboy websocket processes receive erlang messages with the websocket_info/3 function.
因此,例如,websocket处理程序可能是这样的:
So for example, the websocket handler could be like this:
websocket_init(_, Req, _Opts) ->
...
% l is for local process registration
% echo is the name of the event you register the process to
gproc_ps:subscribe(l, echo),
...
websocket_info({gproc_ps_event, echo, Echo}, Req, State) ->
% sending the Echo to the client
{reply, {text, Echo}, Req, State};
回声处理程序如下:
echo(<<"GET">>, Echo, Req) ->
% sending the echo message to the websockets handlers
gproc_ps:publish(l, echo, Echo),
cowboy_req:reply(200, [
{<<"content-type">>, <<"text/plain; charset=utf-8">>}
], Echo, Req);
这篇关于在Cowboy中的HTTP处理程序和websocket处理程序之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!