如何使用 smack API 4.1.0 android 发送一对一消息
How to send one to one message using smack API 4.1.0 android
我正在使用 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();
}
}
}
任何帮助将不胜感激谢谢。
这是我上一个项目发送消息代码的例子:
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();
...
}
}
}
请按照以下几个步骤在 xmpp 中进行一对一通信:
在 StanzaListener 中,您已经获得了在两个人之间添加连接的所有消息,但是如果您想要特定的两人聊天,请使用 ChatMessageListener.
第一步,声明为全局变量
ChatManagerListener chatListener;
ChatMessageListener messageListener;
Chat chat;
private Jid opt_jid;
StanzaListener packetListener;
第 2 步。在 oncreate 或片段中使用此代码
注意:请确保您已连接到聊天服务器。
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();
}
步骤 3. 一对一聊天目的的方法
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) {
}
}
}
步骤 4. 销毁
XMPP.getInstance().removeChatListener(getActivity(), chatListener);
if (chat != null && messageListener != null) {
XMPP.getInstance().getConnection(acitiviy)).removeAsyncStanzaListener(stanzaListener);
chat.removeMessageListener(messageListener);
}
希望这对您有所帮助,如果您需要更多信息,请查看 from here。
谢谢
我正在使用 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();
}
}
}
任何帮助将不胜感激谢谢。
这是我上一个项目发送消息代码的例子:
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();
...
}
}
}
请按照以下几个步骤在 xmpp 中进行一对一通信: 在 StanzaListener 中,您已经获得了在两个人之间添加连接的所有消息,但是如果您想要特定的两人聊天,请使用 ChatMessageListener.
第一步,声明为全局变量
ChatManagerListener chatListener;
ChatMessageListener messageListener;
Chat chat;
private Jid opt_jid;
StanzaListener packetListener;
第 2 步。在 oncreate 或片段中使用此代码
注意:请确保您已连接到聊天服务器。
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();
}
步骤 3. 一对一聊天目的的方法
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) {
}
}
}
步骤 4. 销毁
XMPP.getInstance().removeChatListener(getActivity(), chatListener);
if (chat != null && messageListener != null) {
XMPP.getInstance().getConnection(acitiviy)).removeAsyncStanzaListener(stanzaListener);
chat.removeMessageListener(messageListener);
}
希望这对您有所帮助,如果您需要更多信息,请查看 from here。 谢谢