如何跟踪每个浏览器窗口的进程,并在每个事件中进行氮的访问? [英] How to keep track of a process per browser window and access it at each event in Nitrogen?

查看:149
本文介绍了如何跟踪每个浏览器窗口的进程,并在每个事件中进行氮的访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在氮气中,Erlang的Web框架,我有以下问题。我有一个过程,负责发送和接收消息到作为集线器的另一个进程。此过程充当接收邮件和更新页面的彗星进程。



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



启动通信并设置接收部分的代码看起来像这样,首先我有一个事件开始客户端进程通过调用wf:comet:

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

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

  chat_client() - > 
房间= room_provider:get_room(),
房间:加入(房间),
chat_client(房间)。

chat_client(房间) - >
收到
{send_message,Message} - >
房间:send_message(房间,消息);
{message,From,Message} - >
wf:insert_bottom(message,[#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没有在那里定义,我看不到如何看待它。任何想法?



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

解决方案

Nitrogen提供了一个名为 state 的页面实例的键值存储。从文档



检索存储在指定键下的页面状态值。页面状态与会话状态不同,该页面状态被一个用户限定为一个氮页面的一系列请求:

  wf:state(Key) - >价值

存储当前用户的页面状态变量。页面状态与会话状态不同,该页面状态被一个用户限定为一个氮页面的一系列请求:

  wf:state(Key,Value) - >确定

清除用户的页面状态:

  wf:clear_state() - > OK 


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

解决方案

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

这篇关于如何跟踪每个浏览器窗口的进程,并在每个事件中进行氮的访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆