我们想构建一个简单的用于ejabberd的服务器组件,它接收发送到MUC房间的所有消息(我们有很多房间,并且一直在创建新房间),并且在处理了其中一些之后消息,执行一些操作。
我们不希望我们的服务器组件像机器人一样,所以我们不希望它回复消息或类似的东西,我们只是希望它接收所有消息的副本以处理其中的一些。
为此,我们遵循了此处提供的教程:https://sleekxmpp.readthedocs.io/en/latest/getting_started/component.html
问题是该组件似乎只接收到某些消息(大约五分之一)。
此外,我们正在观察一种奇怪的行为:消息传递似乎是“排他性的”,这意味着消息传递给连接到房间的客户端或服务器组件,这很奇怪。换句话说,5个消息中的1个传递给服务器组件,其余4个消息照常传递给客户端。
这是我们的组件代码(我们已经尝试使用sleekxmpp和slixmpp,但我们始终具有相同的行为):
import sys
import logging
#import sleekxmpp
#from sleekxmpp.componentxmpp import ComponentXMPP
import slixmpp
from slixmpp.componentxmpp import ComponentXMPP
if sys.version_info < (3, 0):
from sleekxmpp.util.misc_ops import setdefaultencoding
setdefaultencoding('utf8')
else:
raw_input = input
class NotificationsComponent(ComponentXMPP):
def __init__(self):
ComponentXMPP.__init__(self, "muc.ourservice.it", "secret", "jabber.ourservice.it", 5233)
# add handler
self.add_event_handler("message", self.message)
#self.add_event_handler("groupchat_message", self.message)
def message(self, msg):
if msg['type'] == 'groupchat':
print('Received group chat message')
print(msg)
#msg.reply('Well received').send()
else:
print('Received another message')
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG,format='%(levelname)-8s %(message)s')
xmpp = NotificationsComponent()
xmpp.register_plugin('xep_0030') # Service Discovery
#xmpp.register_plugin('xep_0004') # Data Forms
#xmpp.register_plugin('xep_0060') # PubSub
xmpp.register_plugin('xep_0199') # XMPP Ping
#xmpp.register_plugin('xep_0045') # MUC
# Connect to the XMPP server and start processing XMPP stanzas.
xmpp.connect()
xmpp.process()
这是我们的ejabberd 18.03配置的摘要:
listen:
-
port: 5222
ip: "::"
module: ejabberd_c2s
starttls: true
certfile: 'CERTFILE'
protocol_options: 'TLSOPTS'
## dhfile: 'DHFILE'
## ciphers: 'CIPHERS'
##
## To enforce TLS encryption for client connections,
## use this instead of the "starttls" option:
##
starttls_required: true
##
## Stream compression
##
zlib: true
##
max_stanza_size: 65536
shaper: none
access: c2s
-
port: 5280
ip: "::"
module: ejabberd_http
request_handlers:
"/admin": ejabberd_web_admin
"/bosh": mod_bosh
#request_handlers:
# "/ws": ejabberd_http_ws
# "/bosh": mod_bosh
# "/api": mod_http_api
## "/pub/archive": mod_http_fileserver
web_admin: true
http_bind: true
## register: true
captcha: false
certfile: 'CERTFILE'
tls: true
-
port: 5233
ip: "::"
module: ejabberd_service
access: all
privilege_access:
message: "outgoing"
password: "secret"
shaper: none
[我们也曾尝试使用access,privilege_access之类的东西,但是没有运气。
您知道什么可能导致这种奇怪的行为吗?是否有任何应启用的特定插件或模块?
当然,我们已经在sleekxmpp和ejabberd上启用了调试日志,但是我们看不到任何错误,只是消息丢失了。
我们还进行了另一项测试。即使使用slixmpp存储库中提供的官方“回声组件”示例,我们也遇到相同的问题。因此,看来我们的服务器存在一些问题,也许在消息路由部分,我们不知道。
谢谢
我认为您在这里混了两件事。从https://xmpp.org/extensions/xep-0114.html判断,您在此处创建的组件似乎作为外部组件(请参阅https://xmpp.org/extensions/xep-0225.html或http://sleekxmpp.com/getting_started/component.html)连接到ejabber,这意味着ejabber(至少似乎)将一些消息路由到其内部组件,而另一些消息则路由到您的(外部)组件。这可以解释为什么您的组件仅接收某些消息。
您有两个选择:
两个选项都有优点和缺点:
事实证明,我完全误解了Jabber外部组件的目的。
我原本希望收到ejabberd内发生的所有事件的“副本”,但我错了。
为了达到我期望的结果,我使用了一个名为“ mod_post_log”的模块,该模块针对用户发送的每条消息发送一个HTTP请求。这对我有用。