0

在从服务器向一个用户的所有活动客户端发送消息时,发送者地址未正确写入。

这是广播功能(服务器端):

private void sendAllSessions(final StringBuilder message, final Entity sender,
        final ServerRuntimeContext serverContext, EMailAddress to) {
    EntityImpl recipient = new EntityImpl(to.getLocalName().getLocalName(), to.getDomain().getDomainName(), null);
    Stanza build = createStanza(message, sender, recipient);
    for (SessionContext sessionContext : serverContext.getResourceRegistry().getSessions(recipient)) {
        SessionState state = sessionContext.getState();
        SessionStateHolder stateHolder = new SessionStateHolder();
        stateHolder.setState(state);
        Stanza stanza = new MessageStanza(build);

        LOG.severe("Send xmpp stanza: " + stanza + " from " + stanza.getFrom());
        serverContext.getStanzaProcessor().processStanza(serverContext, sessionContext, stanza, stateHolder);
    }
}

private Stanza createStanza(final StringBuilder strb, final Entity sender, final EntityImpl recipient) {
    try {
        LOG.severe("Create xmpp stanza from " + sender + " (" + sender.getFullQualifiedName() + ") to recipient "
                + recipient + " (" + recipient.getFullQualifiedName() + ")!");
        StanzaBuilder sb = StanzaBuilder.createMessageStanza(sender, recipient, MessageStanzaType.HEADLINE, "html",
                strb.toString());
        Stanza build = sb.build();
        return build;
    } catch (RuntimeException e) {
        StanzaBuilder sb = StanzaBuilder.createMessageStanza(sender, recipient, MessageStanzaType.NORMAL, null,
                strb.toString());
        Stanza build = sb.build();
        return build;
    }
}

这是日志记录(服务器端):

07-Oct-2021 12:43:58.220 SEVERE [http-nio-80-exec-147] xx.DefaultChat.createStanza Create xmpp stanza from release-ma@example.com (release-ma@example.com) to recipient admin@example.com (admin@example.com)!
07-Oct-2021 12:43:58.222 SEVERE [http-nio-80-exec-147] xx.DefaultChat.sendAllSessions Send xmpp stanza: message.body.Thanks, please wait... from release-ma@example.com
07-Oct-2021 12:43:58.229 FINE [NioProcessor-2] org.apache.vysper.mina.StanzaLoggingFilter.messageSent ><message xmlns="jabber:client" to="release-ma@example.com" from="example.com" type="error"><body>Thanks, please wait...</body><error type="modify"><unknown-sender xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></unknown-sender></error></message>

人类可读的 xml 是(从服务器到客户端):

<message xmlns="jabber:client" to="release-ma@example.com"
    from="example.com" type="error">
    <body>Thanks, please wait...</body>
    <error type="modify">
        <unknown-sender
            xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></unknown-sender>
    </error>
</message>

我的问题是为什么 XML-Stanza 指出发件人(来自字段)只是域?

4

1 回答 1

0

这是来自服务器的错误回复,因此发送此消息的是服务器,而不是特定用户。

如果您查看处理错误的源代码:

StanzaBuilder responseBuilder = StanzaBuilder.createDirectReply(stanza, true, "error");

第二个参数是“fromIsServerOnly”参数:

public static StanzaBuilder createDirectReply(XMPPCoreStanza original, boolean fromIsServerOnly, String type)

在这种情况下,创建的 Stanza 会将发件人地址剥离为仅域:

if (fromIsServerOnly)
   newFrom = new EntityImpl(null, newFrom.getDomain(), null);
stanzaBuilder.addAttribute("from", newFrom.getFullQualifiedName());
于 2021-10-15T15:51:05.837 回答