目前我有一个运行的Spring Boot应用程序,它以特定用户身份登录到XMPP服务器。我可以向远程客户端等发送消息。但是现在我想在服务器端接收消息,如果客户端正在向通过Spring Boot登录的特定用户发送消息。
我尝试连接实际接收消息的服务激活器,但消息内容被放入“to”字段,“from”属性为null。所以我很困惑如何挂钩......
以下是传入消息适配器的xmpp配置:
<!-- incoming xmpp configuration -->
<int-xmpp:inbound-channel-adapter auto-startup="true" xmpp-connection="xmppConnection" channel="incomingMessagesChannel"/>
<channel id="incomingMessagesChannel">
<interceptors>
<wire-tap channel="incomingMessagesLogger"/>
</interceptors>
</channel>
<logging-channel-adapter id="incomingMessagesLogger" logger-name="incoming logger" log-full-message="true" />
<service-activator ref="messageEventHandler" method="handle" input-channel="incomingMessagesChannel"/>
处理传入消息的类:
import org.jivesoftware.smack.packet.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class MessageEventHandler {
private static final Logger logger = LoggerFactory.getLogger(MessageEventHandler.class);
public void handle(Message message) {
System.out.println(message.getFrom());
System.out.println(message.getTo());
System.out.println(message.getBody());
}
}
所以当我向服务器发送消息时输出:
System.out.println(message.getFrom());
是null
System.out.println(message.getTo());
是实际的消息内容System.out.println(message.getBody());
是null
我错过了什么?
答案 0 :(得分:2)
解决此问题的方法是使用正确的Message实现!
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;
@Component
public class MessageEventHandler {
private static final Logger logger = LoggerFactory.getLogger(MessageEventHandler.class);
public void handle(Message message) {
MessageHeaders headers = message.getHeaders();
String from = (String) headers.get("xmpp_from");
String to = (String) headers.get("xmpp_to");
Long timestamp = (Long) headers.get("timestamp");
String payload = (String) message.getPayload();
logger.debug("Received a message from: {} with payload: {}", from, payload);
}
}