我正在使用smack API官方文档中的代码将消息发送到特定的Jabber ID CLick Here
我可以使用以下代码从房间接收消息。
public void joinChatRoom(){
MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);
multiUserChat = manager.getMultiUserChat("test@-mbp-9");
try {
multiUserChat.join("user");
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
ChatManager.getInstanceFor(connection).addChatListener(new ChatManagerListener() {
@Override
public void chatCreated(Chat chat, boolean createdLocally) {
chat.addMessageListener(new ChatMessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
System.out.println(message.getBody());
}
});
}
});
System.out.println("Test");
}
我的问题是我如何向特定的JID发送消息,因为即使经过大量谷歌搜索后我也无法解决这个问题。连接很好用户也在验证,但是下面的代码现在正在用于发送消息。
public void sendMsg() {
if (connection.isConnected()) {
// Assume we've created an XMPPConnection name "connection"._
chatmanager = ChatManager.getInstanceFor(connection);
newChat = chatmanager.createChat("user123@csofts-mbp-9", new ChatMessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
System.out.println("Received Message:"+message);
}
});
try {
System.out.println("check the message....");
newChat.sendMessage("Howdy!alksd;lsakdsa;lkdsa;lksa;lsa");
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
}
}
任何帮助将不胜感激。
答案 0 :(得分:2)
以下是从我上一个项目发送消息代码的示例:
private void sendMessage(String body, String toJid) {
try {
Jid jid = JidCreate.from(toJid + "@" + MyApplication.CHAT_DOMAIN);
Chat chat = ChatManager.getInstanceFor(mConnection)
.createChat(jid.asJidWithLocalpartIfPossible());
chat.sendMessage(body);
} catch (Exception e) {
}
}
<强>更新强>
如何接收讯息:
经过身份验证后:
ChatManager.getInstanceFor(mConnection).addChatListener(YourConnectionClass.this);
(YourConnectionClass应实现ChatManagerListener和ChatMessageListener)
您还需要实现以下方法:
@Override
public void chatCreated(Chat chat, boolean b) {
chat.addMessageListener(ChatConnection.this);
}
@Override
public void processMessage(Chat chat, Message message) {
if (message.getType().equals(Message.Type.chat) || message.getType().equals(Message.Type.normal)) {
if (message.getBody() != null) {
Jid jid = message.getFrom();
Localpart localpart = jid.getLocalpartOrNull();
...
}
}
}
答案 1 :(得分:1)
请按照以下几个步骤进行xmpp中的一对一通信: 在 StanzaListener 中,您已经获得了在两个人之间添加连接的所有消息,但如果您想要特定的双人聊天,请使用 ChatMessageListener 。
步骤1.声明为全局变量
UPDATE t1 SET t1.val1 = SUM(t2.val2)
FROM table1
INNER JOIN table2 t2 ON t2.col1 = t1.col1
and t2.col2 = t1.col2
and t2.col3 = t1.col3
and t2.col4 = t2.col4
步骤2.在oncreate或片段中使用此代码
注意:确保已连接聊天服务器。
ChatManagerListener chatListener;
ChatMessageListener messageListener;
Chat chat;
private Jid opt_jid;
StanzaListener packetListener;
步骤3.一对一聊天用户的方法
try {
String opt_jidStr = "user_" + userid;
try {
opt_jid = JidCreate.bareFrom(Localpart.from(opt_jidStr), Domainpart.from(Common.HOST));
PurplkiteLogs.logError(TAG,"opt jid :" + opt_jid);
} catch (XmppStringprepException e) {
e.printStackTrace();
}
messageListener = new ChatMessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
AppLogs.logInfo(TAG, "chat get me something " + message.getBody());
}
};
packetListener = new StanzaListener() {
@Override
public void processPacket(Stanza packet) throws SmackException.NotConnectedException, InterruptedException {
if (packet instanceof Message) {
final Message message = (Message) packet;
}
}
};
chatListener = new ChatManagerListener() {
@Override
public void chatCreated(Chat chatCreated, boolean local) {
onChatCreated(chatCreated);
}
};
XMPP.getInstance().getConnection(acitiviy)).addAsyncStanzaListener(stanzaListener, null);
ChatManager.getInstanceFor(XMPP.getInstance().getConnection(acitiviy)))
.addChatListener(chatManagerListener);
ServiceDiscoveryManager sdm = ServiceDiscoveryManager
.getInstanceFor(XMPP.getInstance().getConnection(acitiviy)));
sdm.addFeature("jabber.org/protocol/si");
sdm.addFeature("http://jabber.org/protocol/si");
sdm.addFeature("http://jabber.org/protocol/disco#info");
sdm.addFeature("jabber:iq:privacy");
try {
String addr1 = XMPP.getInstance().getUserLocalPart(getActivity());
String addr2 = opt_jid.toString();
if (addr1.compareTo(addr2) > 0) {
String addr3 = addr2;
addr2 = addr1;
addr1 = addr3;
}
chat = ChatManager.getInstanceFor(
XMPP.getInstance().getConnection(acitiviy)))
.getThreadChat(party1 + "-" + party2);
AppLogs.logInfo(TAG, "chat value single chat :" + chat + " , " + addr2 + " , " + addr1);
// for subscribed the user
Presence subscribe = new Presence(Presence.Type.subscribe);
subscribe.setTo(opt_jidStr);
XMPP.getInstance().getConnection(acitiviy)).sendStanza(packet);
// for subscribed the user
if (chat == null) {
chat = ChatManager.getInstanceFor(
XMPP.getInstance().getConnection(acitiviy))
.createChat(jid, party1 + "-" + party2,
messageListener);
AppLogs.logInfo(TAG, "chat value single chat 1 :" + chat);
} else {
chat.addMessageListener(messageListener);
AppLogs.logInfo(TAG, "chat value single chat 2:" + chat);
}
} catch (Exception e) {
e.printStackTrace();
}
} catch(Exception e) {
e.printStackTrace();
}
步骤4.销毁
void onChatCreated(Chat chatCreated) {
if (chat != null) {
if (chat.getParticipant().getLocalpart().toString().equals(
chatCreated.getParticipant().getLocalpart().toString())) {
chat.removeMessageListener(messageListener);
chat = chatCreated;
chat.addMessageListener(messageListener);
}
} else {
chat = chatCreated;
chat.addMessageListener(messageListener);
}
}
void sendMessage(String message) {
if (chat != null) {
try {
chat.sendMessage(message);
} catch (SmackException.NotConnectedException e) {
} catch (Exception e) {
}
}
}
希望这会对您有所帮助,如果您想了解更多信息,请查看from here。 三江源