如何跟踪每个浏览器窗口的进程并在 Nitrogen 中的每个事件中访问它?

发布于 2024-08-16 19:47:04 字数 1184 浏览 8 评论 0原文

在 Nitrogen(Erlang Web 框架)中,我遇到以下问题。我有一个进程负责向充当集线器的另一个进程发送和接收消息。该进程充当彗星进程来接收消息并更新页面。

问题是,当用户处理按钮时,我会收到对事件的调用。我如何在活动中获得该 Pid。

启动通信并设置接收部分的代码如下所示,首先我有一个通过调用 wf:comet 启动客户端进程的事件:

event(start_chat) -> 
  Client = wf:comet(fun() -> chat_client() end);

客户端进程的代码如下,它获取并加入一个房间开始,然后进入一个循环,向房间发送消息和从房间接收消息:

chat_client() -> 
  Room = room_provider:get_room(), 
  room:join(Room), 
  chat_client(Room).

chat_client(Room) -> 
  receive
    {send_message, Message} ->
      room:send_message(Room, Message);
    {message, From, Message} -> 
      wf:insert_bottom(messages, [#p{}, #span { text=Message }]), 
      wf:comet_flush()
  end, 
  chat_client(Room).

现在,问题来了。我有另一个事件,send_message:

event(send_message) ->
  Message = wf:q(message),
  ClientPid ! {send_message, Message}.

除了 ClientPid 没有在那里定义,而且我不知道如何获取它。有什么想法吗?

Nitrogen 邮件列表中的相关威胁:http://groups.google.com /group/nitrogenweb/browse_thread/thread/c6d9927467e2a51a

In Nitrogen, the Erlang web framework, I have the following problem. I have a process that takes care of sending and receiving messages to another process that acts as a hub. This process acts as the comet process to receive the messages and update the page.

The problem is that when the user process a button I get a call to event. How do I get ahold of that Pid at an event.

the code that initiates the communication and sets up the receiving part looks like this, first I have an event which starts the client process by calling wf:comet:

event(start_chat) -> 
  Client = wf:comet(fun() -> chat_client() end);

The code for the client process is the following, which gets and joins a room at the beginning and then goes into a loop sending and receiving messages to/from the room:

chat_client() -> 
  Room = room_provider:get_room(), 
  room:join(Room), 
  chat_client(Room).

chat_client(Room) -> 
  receive
    {send_message, Message} ->
      room:send_message(Room, Message);
    {message, From, Message} -> 
      wf:insert_bottom(messages, [#p{}, #span { text=Message }]), 
      wf:comet_flush()
  end, 
  chat_client(Room).

Now, here's the problem. I have another event, send_message:

event(send_message) ->
  Message = wf:q(message),
  ClientPid ! {send_message, Message}.

except that ClientPid is not defined there, and I can't see how to get ahold of it. Any ideas?

The related threat at the Nitrogen mailing list: http://groups.google.com/group/nitrogenweb/browse_thread/thread/c6d9927467e2a51a

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

困倦 2024-08-23 19:47:04

Nitrogen 为每个页面实例提供一个键值存储,称为“状态”。从文档

检索存储在指定键下的页面状态值。页面状态与会话状态的不同之处在于,页面状态的范围是一个用户对一个氮页面的一系列请求:

wf:state(Key) -> Value

存储当前用户的页面状态变量。页面状态与会话状态的不同之处在于,页面状态的范围仅限于一个用户对一个氮页面的一系列请求:

wf:state(Key, Value) -> ok

清除用户的页面状态:

wf:clear_state() -> ok

Nitrogen provides a key-value storage per page instance called state. From the documentation:

Retrieve a page state value stored under the specified key. Page State is different from Session State in that Page State is scoped to a series of requests by one user to one Nitrogen Page:

wf:state(Key) -> Value

Store a page state variable for the current user. Page State is different from Session State in that Page State is scoped to a series of requests by one user to one Nitrogen Page:

wf:state(Key, Value) -> ok

Clear a user's page state:

wf:clear_state() -> ok
诺曦 2024-08-23 19:47:04

有一个 ets 表,它将会话 id 映射到客户端 Pid。或者,如果氮提供任何类型的会话管理,则将 Pid 存储为会话数据。

Have an ets table which maps session id's to client Pid's. Or if nitrogen provides any sort of session management, store the Pid as session data.

执手闯天涯 2024-08-23 19:47:04

凡是需要记住的事情都需要一个过程。您的客房提供商似乎并非如此。

room:join(Room) 需要是 room:join(Room,self())。房间需要知道你的彗星进程 pid 是什么。

要向客户端发送消息,您首先将消息发送到房间,然后房间将向房间中的所有客户端发送消息。但要让它发挥作用。每个加入房间的客户端都需要提交 comet-pid。房间需要保存房间中所有 pid 的列表。

Every thing that needs to be remembered needs a process. It looks like your room provider isn't.

room:join(Room) need to be room:join(Room,self()). The room need to know what your comet-process pid is.

To send a message to a client you first send the message to the room, the room will then send a message to all clients in the room. But for that to work. Every client joining the room need to submit the comet-pid. The room need to keep a list of all pid's in the room.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文