将XMPPPresence更改为Away / Busy / Invisible
你如何改变你的存在以显示dnd / away等?
XMPPPresence *presence = [XMPPPresence presenceWithType:status]; [[[self appDelegate] xmppStream] sendElement:presence];
status
是我设置为online / unavailable / away / busy / invisible的NSString
。
它仅在我上线和/或不可用时才有效。
以下是在我的xmppStream
发送状态后的xmppStream
:
要更改客户端的状态,您需要使用以下简单代码:
XMPPPresence *presence = [XMPPPresence presence]; NSXMLElement *status = [NSXMLElement elementWithName:@"status"]; [status setStringValue:@"online/unavailable/away/busy/invisible"]; [presence addChild:status]; [[self xmppStream] sendElement:presence];
这只是意味着更改客户端状态的关键是向您的状态添加状态元素。 请注意,当您将鼠标hover在管理面板中的用户图标上时,openfire服务器将仅显示“可用/离线”状态。 这不应该让你感到困惑。 您只需检查客户端发送的状态消息,并由其他人接收,这些消息将显示您已设置的状态(“在线/不可用/离开/忙碌/不可见”)。
在上面的答案之上,还有一个
元素应该与
元素一起使用。 通过使用这两个元素,您可以为每个可用性状态自定义用户的状态。
默认值:可用/离线
使用
:可用/忙/离/远离/离线
通过
使用
:“Free to chat”/“Hard at work”/“In a meeting”/“Out for lunch”。
如果您使用此方法使用Openfire:在“用户会话”>“状态”列中,您将看到:
-
每个用户的不同颜色图标(例如绿色可用 ,红色表示忙碌等)
-
图标旁边的描述性文字(例如“ 在会议中 ”)
存在子元素
有3个元素可以改变XMPP中的存在类型。
-
-
-
( 我们将其排除在讨论之外 )
显示
指定用户的可用性状态。
必须根据下面的列表指定元素的值。
"chat" -- user is actively interested in chatting. "dnd" -- user is busy (dnd aka 'Do Not Disturb'). "away" -- user is temporarily away. "xa" -- user is away for an extended period (xa aka 'eXtended Away').
如果未提供此元素,则假定用户仅在线且可用。
状态
描述用户的可用性状态。 它通常与
元素一起使用,以提供可用性状态的详细描述。
元素的值可以是任何描述性文本。 例如:
"Available to chat" -- can be used for "chat" "Busy at work" -- can be used for "dnd" "In a meeting" -- can be used for "away" "On a vacation" -- can be used for "xa"
Objective-C中的用法
以下是您应该如何在代码中应用上述概念。
// Initialize variables XMPPPresence *presence = [XMPPPresence presence]; NSXMLElement *show = [NSXMLElement elementWithName:@"show"]; NSXMLElement *status = [NSXMLElement elementWithName:@"status"]; // If user is available [show setStringValue:@"chat"]; [status setStringValue:@"Available to chat"]; // If user is busy [show setStringValue:@"dnd"]; [status setStringValue:@"Busy at work"]; // If user is away [show setStringValue:@"away"]; [status setStringValue:@"In a meeting"]; // If user is away for a long period of time [show setStringValue:@"xa"]; [status setStringValue:@"On a vacation"]; // Add the XML child elements to XMPPPresence [presence addChild:show]; [presence addChild:status]; // Update new presence to server [[[self appDelegate] xmppStream] sendElement:presence];
在那里,您的自定义用户的存在现在将准确地反映在您的服务器中。
另请参阅: 可扩展消息传递和在线协议(XMPP):即时消息和在线状态