为 IBM Lotus Domino 应用程序添加提醒功能(2)
- UID
- 1066743
|
为 IBM Lotus Domino 应用程序添加提醒功能(2)
通过 refreshUserMsg 代理维护在线用户列表refreshUserMsg 代理由 Awareness.js 脚本所创建的 iFrame 载入。该代理将完成以下三个任务:
- 更新用户的在线状态。
- 检查是否存在要传递的消息,如果存在,则传递这些消息。
- 设置定时器,用于在一段时间后重新载入代理。(此功能将确保代理保持对新消息的轮询。如果未执行此任务,则仅当用户重新载入或更改 Web 应用程序中的页面时,消息才会传递。)
清单 1 展示了代理的 Initialize 部分。
清单 1. refreshUserMsg 代理的 Initialize 部分1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| Sub Initialize
Dim session As New notessession
Dim doc As notesDocument
Dim db As NotesDatabase
Set db = session.CurrentDatabase
Set doc = session.documentContext
Print "Content-type:text/html"
' Format a nice header telling the content type.
Print ""
Print |<SCRIPT Language="JavaScript">|
Print |<!--|
' Check if user is registered. Set status.
Call RefreshUser(db, doc)
' Check for messages.
Call Check_Messages(db, doc)
' Check for messages regularly.
' Specify your polling time here in milliseconds.
Print |timerID = setTimeout("document.location.reload()",1000);|
Print |//-->|
Print |</script>|
End Sub
|
第一个和第二个 Print 行设置了 Web 浏览器中对象的内容类型。采用这种方式,Lotus Domino 没有插入默认的 HTML 标题,这使请求节省了几个字节。
Text/html 与 text/javascript有些读者可能会反对不使用内容类型 text/javascript,但在目前的大多数 Web 浏览器中,使用内容类型 text/javascript 将触发安全性警告。显然,在 iFrame 中使用 JavaScript 代码是不安全的,但是在其上使用带有 JavaScript 代码的 HTML 页面是安全的。
清单 2 会在每个用户首次访问系统时为其创建一个文档。然后,对于每一个请求,代码将更新用户文档。如果对用户状态文档的修改未超过两分钟,则认为该用户处于在线状态。
请注意对 RefreshUser 的调用(将维持在线用户列表)以及对 Check_Messages 子函数的调用。代理以设置定时器 setTimeout 结束,此定时器将在 1000 毫秒后重新载入 iFrame。
清单 2. RefreshUser 子函数1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| Sub RefreshUser(db As NotesDataBase, doc As NotesDocument)
Dim uDoc As NotesDocument
Dim view As NotesView
Set view = db.GetView("Users")
' Find the users status document.
Set uDoc = view.GetDocumentByKey(doc.Remote_User(0), True)
If Not uDoc Is Nothing Then ' Set the users status to logged in.
uDoc.status = "Online"
Else ' Create a status document for first timers.
Set uDoc = db.CreateDocument
uDoc.Form = "user"
Dim userName As New NotesName(doc.Remote_User(0))
uDoc.userCN = userName.Common
uDoc.user = userName.Canonical
uDoc.status = "Online"
End If
Call uDoc.Save(True, True)
End Sub
|
如清单 3 所示,代理的最后一个子函数将核对用户名和要传递的任何未处理的 e-msg 表单。如果有相匹配的内容,则代理打开包含消息的弹出窗口。注意 Print 行,此行设置了用于打开消息的 JavaScript 代码。
清单 3. Check_Messages 子函数1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| Sub Check_Messages(db As NotesDatabase, doc As NotesDocument)
Dim view As NotesView
Set view = db.GetView("e-msg (not delivered)")
' Check for messages.
Dim mDoc As NotesDocument
Set mDoc = view.GetDocumentByKey(doc.Remote_User(0), True)
If Not mDoc Is Nothing then ' User got messages!
mDoc.delivered = "1" ' Flag as delivered.
Call mDoc.Save(False, False)
' Bring up the pop-up.
doc.thisDb = Evaluate(|@WebDbName|)
thisDb = doc.thisDb(0)
Print |iMsg = window.open('/| + thisDb +
|/e-msg?openForm&to=| + mDoc.fromEncoded(0) + |&unid=|
+ mDoc.UniversalID + |',
'','height=220,width=250,resizable=yes,left=500,top=60');|
End If
End Sub
|
|
|
|
|
|
|