我一直在尝试使用smack 4.1创建一个Android聊天应用程序。消息发送和接收工作正常,但问题是在mXmppConnection.addAsyncStanzaListener中多次获取消息。我不知道我是否错过了向连接添加内容。
这是我的连接类:
XMPPTCPConnectionConfiguration.Builder configBuilder = new XMPPTCPConnectionConfiguration.builder();
configBuilder.setUsernameAndPassword(mUser, "password@123");
configBuilder.setPort(5555);
configBuilder.setServiceName("tvm.myname.com");
configBuilder.setDebuggerEnabled(true); configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
try
{
XMPPTCPConnection mConnection = new XMPPTCPConnection(configBuilder.build());
mConnection.connect();
mConnection.login();
}
catch (SmackException e)
{
}
mXmppConnection.addAsyncStanzaListener(new StanzaListener() {
@Override
public void processPacket(Stanza packet) throws SmackException.NotConnectedException {
Message message = (Message)packet;
Log.i("XMPPClient", "****** message " + message);
// code for handling message
} `enter code here`
},null);
真正的问题是我多次收到消息..消息的价值是在日志中打印好几次。请帮帮我......
答案 0 :(得分:3)
最终解决方案
问题不在于客户端,而是由于粗心编码。我一直在为类变量分配连接对象的单个实例,并且每次都会向这些引用对象添加侦听器。因此,这会导致多次调用侦听器....修复是通过向单例连接对象添加侦听器来完成的。
答案 1 :(得分:1)
我不知道有没有最好的办法让xmpp连接在应用程序中保持稳定。如果有人知道更好的解决方案,请在此处发布答案。我正在使用全局连接变量,所有聊天操作都是通过使用此静态连接变量来完成的。这对我有用。 要使用smack发送和接收消息,我们需要与xmpp服务器建立连接。
public static AbstractXMPPConnection getInstance(Context context) {
mContext = context;
sendMessageCallBack = (XmppSendMessageCallBack) context;
if (mConnection == null) {
mInstance = new XmppClient();
mUser = new Preferences(context).getPhone();
setUserToServer();
}
return mConnection;
}
private static void setUserToServer() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Looper.prepare();
/** connecting to server ***/
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setServiceName(Constants.HOST_URL);
configBuilder.setDebuggerEnabled(true);
configBuilder.setSendPresence(true);
configBuilder.setConnectTimeout(XMPPTCPConnectionConfiguration.DEFAULT_CONNECT_TIMEOUT);
configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
String[] sslProtocols = {"starttls"
, "no_sslv3"
, "no_tlsv1"};
configBuilder.setEnabledSSLProtocols(sslProtocols);
mConnection = new XMPPTCPConnection(configBuilder.build());
mConnection.setPacketReplyTimeout(120000);
mConnection.connect();
// Log into the server
if(mConnection!=null) {
mConnection.login(mUser, "password@123");
reConnectionSetUp();
PingManager pingManager = PingManager.getInstanceFor(mConnection);
pingManager.setPingInterval(60000);
pingManager.pingMyServer();
pingManager.registerPingFailedListener(new PingFailedListener() {
@Override
public void pingFailed() {
if (mConnection != null && !mConnection.isConnected())
setUserToServer();
}
});
setUpListenersForXmppConnection(mConnection);
}
} catch (SmackException.ConnectionException e) {
Log.e("XmppClient", "ConnectionException :", e);
Toast.makeText(mContext,"failed to connect to server",Toast.LENGTH_SHORT).show();
} catch (SmackException.NoResponseException e) {
Log.e("XmppClient", "NoResponseException :", e);
Toast.makeText(mContext,"Connection time out please try again",Toast.LENGTH_SHORT).show();
} catch (XMPPException e) {
Log.e("XmppClient", "XMPPException :", e);
}catch (IOException e) {
Log.e("XmppClient", "IOException :", e);
}catch (SmackException.NotConnectedException e) {
Log.e("XmppClient", "NotConnectedException :", e);
reConnectionSetUp();
}catch (SmackException e) {
Log.e("XmppClient", "SmackException :", e);
}catch (NullPointerException e) {
Log.e("XmppClient", "NullPointerException :", e);
}
}
}).start();
}
private static void setUpListenersForXmppConnection(AbstractXMPPConnection xmppConnection){
try {
if(xmppConnection!=null) {
sendOnlineStatus();
/** adding connection listener **/
xmppConnection.addConnectionListener(mInstance);
/** adding privacy manager to connection **/
if(xmppConnection!=null)
mPrivacyListManager = PrivacyListManager.getInstanceFor(xmppConnection);
/** adding packet listener for receving incoming packets **/
StanzaFilter filter = MessageTypeFilter.NORMAL;
if(xmppConnection!=null && mInstance!=null)
xmppConnection.addSyncStanzaListener(mInstance, null);
}
} catch (SmackException e) {
Log.e("XmppClient", "IOException :", e);
} catch (XMPPException e) {
Log.e("XmppClient", "XMPPException :", e);
e.printStackTrace();
} catch (NullPointerException e) {
Log.e("XmppClient", "NullPointerException :", e);
} catch (ConcurrentModificationException e){
Log.e("XmppClient", "ConcurrentModificationException :", e);
} catch (IllegalArgumentException e){
e.printStackTrace();
}catch (RetrofitError e){
Log.e("XmppClient", "RetrofitError :", e);
}
}
当我们收到消息时,将调用以下方法
@Override
public void processPacket(Stanza packet) throws SmackException.NotConnectedException {
if(packet instanceof Message) {
Message message = (Message) packet;
// Do your task
}
}
我们可以通过像这样创建一个smack的消息对象来发送消息,
Message message = new Message();
message.setFrom(senderId);
message.setBody(body);
message.setSubject(subject);
message.setTo(receiverId);
try {
if(mConnection!=null){
ChatManager chatManager = ChatManager.getInstanceFor(mConnection);
if(chatManager!=null){
chatManager.createChat(message.getTo(), new ChatStateListener() {
@Override
public void stateChanged(Chat chat, ChatState state) {
Log.e("XMPPClient", "******* stateChanged "+state);
}
@Override
public void processMessage(Chat chat, Message message) {
Log.e("XMPPClient", "******* processMessage "+message.getSubject());
}
}).sendMessage(message);
}
}
sendMessageCallBack.messageSuccessfullySend(message.getStanzaId(), status);
}catch (SmackException.NotConnectedException e){
Log.e("XMPPClient", "******* NotConnectedException ", e);
sendMessageCallBack.messageSendingFailed("");
}catch(NullPointerException e){
Log.e("XMPPClient", "******* NullPointerException ", e);
sendMessageCallBack.messageSendingFailed("");
}catch (Exception e){
sendMessageCallBack.messageSendingFailed("No Network");
}
答案 2 :(得分:1)
我面临同样的问题,但是我得到了解决方案,当你从下面的应用程序注销时,你没有取消注册你的BroadcastReceiver是注销代码,你也使所有连接都是单例。
try {
Presence pr=new Presence(Presence.Type.unavailable);
pr.setStatus(RoosterConnection.getConnection().getUser() + "false");
RoosterConnection.getConnection().sendStanza(pr);
if (mConnection != null) {
mConnection.disconnect();
}
// mBus.unregister(this);
mConnection = null;
// Unregister the message broadcast receiver.
if (uiThreadMessageReceiver != null) {
mApplicationContext.unregisterReceiver(uiThreadMessageReceiver);
uiThreadMessageReceiver = null;
}
Intent intent = new Intent(this,LoginActivity.class);
startActivity(intent);
finish();}catch(SmackException.NotConnectedException e){ e.printStackTrace();}catch(InterruptedException e){
e.printStackTrace();}
答案 3 :(得分:0)
只需使用addSyncStanzaListener
代替addAsyncStanzaListener