我正在尝试构建一个聊天应用程序,我已成功实现了单个聊天和群聊。但是,无论何时关闭wifi,连接都会按预期断开连接,但是当wifi再次打开时,关闭时发送给用户的所有消息都将丢失。我尝试了很多东西,但我没有得到答案。
以下是我的代码:
public class ChatConnection extends AsyncTask<Void, Void, Void> implements Constants
{
private String userId ;
public XMPPConnection connection;
private Context context;
public ChatConnection(Context context)
{
// this.userId = "2";
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params)
{
// Create a connection
ConnectionConfiguration connConfig = new ConnectionConfiguration(CHATHOST, PORT);
connConfig.setSecurityMode(SecurityMode.disabled);
connConfig.setDebuggerEnabled(true);
connection = new XMPPTCPConnection(connConfig);
try {
connection.connect();
Log.i("XMPPChatDemoActivity", "[SettingsDialog] Connected to "+connection.getHost());
if(connection.isConnected())
{
Map<String, String> attributes = new HashMap<>();
attributes.put("username", USERID);
attributes.put("password", PASSWORD);
Registration reg = new Registration();
reg.setType(IQ.Type.SET);
reg.setTo(connection.getServiceName());
reg.setAttributes(attributes);
connection.sendPacket(reg);
connection.login(USERID, PASSWORD);
Log.i("XMPPChatDemoActivity", "Logged in as" + connection.getUser());
//Set the status to available
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries)
{
Log.d("XMPPChatDemoActivity", "--------------------------------------");
Log.d("XMPPChatDemoActivity", "RosterEntry " + entry);
Log.d("XMPPChatDemoActivity", "User: " + entry.getUser());
Log.d("XMPPChatDemoActivity", "Name: " + entry.getName());
Log.d("XMPPChatDemoActivity", "Status: " + entry.getStatus());
Log.d("XMPPChatDemoActivity", "Type: " + entry.getType());
Presence entryPresence = roster.getPresence(entry.getUser());
Log.d("XMPPChatDemoActivity", "Presence Status: "+ entryPresence.getStatus());
Log.d("XMPPChatDemoActivity", "Presence Type: " + entryPresence.getType());
Presence.Type type = entryPresence.getType();
if (type == Presence.Type.available)
Log.d("XMPPChatDemoActivity", "Presence AVIALABLE");
Log.d("XMPPChatDemoActivity", "Presence : " + entryPresence);
}
}
}
catch (XMPPException ex)
{
Log.e("XMPPChatDemoActivity", "[SettingsDialog] Failed to connect to "+ connection.getHost());
Log.e("XMPPChatDemoActivity", ex.toString());
connection = null;
}
catch (SmackException e)
{
e.printStackTrace();
}
catch (IOException e)
{
Log.e("IO Exception",e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if(connection != null)
{
XMPPLogic.getInstance().setConnection(connection);
MultiUserChat muc = new MultiUserChat(connection, 13+"@"+GROUP_CHAT);
try {
DiscussionHistory history = new DiscussionHistory();
history.setMaxStanzas(0);
muc.join(USERID +"@"+ CHATHOST,null,history, SmackConfiguration.getDefaultPacketReplyTimeout());
} catch (NoResponseException e) {
e.printStackTrace();
} catch (XMPPErrorException e) {
e.printStackTrace();
} catch (NotConnectedException e) {
e.printStackTrace();
}
// createChatGroup();
PacketFilter groupFilter = new MessageTypeFilter(Message.Type.groupchat);
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
String toName = StringUtils.parseBareAddress(message.getTo());
String chatSubject = StringUtils.parseBareAddress(message.getSubject());
String messageBody = message.getBody();
Log.i("XMPPChatDemoActivity ", " Text Recieved " + message.getBody() + " from " + fromName);
String[] fromSplit = fromName.split("@");
String[] toSplit = toName.split("@");
String receiverId = toSplit[0];
String senderId = fromSplit[0];
Intent chatIntent = new Intent("new-chat");
chatIntent.putExtra("receiverId", receiverId);
chatIntent.putExtra("senderId", senderId);
chatIntent.putExtra("messageBody", messageBody);
LocalBroadcastManager.getInstance(context).sendBroadcast(chatIntent);
}
}
}, filter);
connection.addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) throws NotConnectedException {
Log.e("Group Message", "Received");
Message message = (Message) packet;
if (message.getBody() != null) {
String groupName = StringUtils.parseBareAddress(message.getFrom());
String fromName = StringUtils.parseResource(message.getFrom());
String toName = StringUtils.parseBareAddress(message.getTo());
String chatSubject = StringUtils.parseBareAddress(message.getSubject());
String messageBody = message.getBody();
Log.i("XMPPChatDemoActivity ", " Text Recieved " + message.getBody() + " from " + fromName + " to " + toName);
String[] groupSplit = groupName.split("@");
String[] toSplit = toName.split("@");
String[] fromSplit = fromName.split("@");
String receiverId = toSplit[0];
String fromFirstSubString = fromSplit[0];
String senderId = groupSplit[0];
String originalGroup = groupSplit[0];
if (!fromFirstSubString.equals(receiverId)) {
Intent chatIntent = new Intent("new-chat");
chatIntent.putExtra("senderId", senderId);
chatIntent.putExtra("receiverId", receiverId);
chatIntent.putExtra("messageBody", messageBody);
LocalBroadcastManager.getInstance(context).sendBroadcast(chatIntent);
}
}
}
}, groupFilter);
}
}
以下是我关闭Wifi时的例外情况:
W / PacketWriter:写入关闭流元素的异常 java.net.SocketException:sendto failed:EPIPE(Broken pipe) 在libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:506) 在libcore.io.IoBridge.sendto(IoBridge.java:475) 在java.net.PlainSocketImpl.write(PlainSocketImpl.java:507) 在java.net.PlainSocketImpl.access $ 100(PlainSocketImpl.java:46) 在 java.net.PlainSocketImpl $ PlainSocketOutputStream.write(PlainSocketImpl.java:269) at java.io.OutputStreamWriter.flushBytes(OutputStreamWriter.java:167) 在java.io.OutputStreamWriter.flush(OutputStreamWriter.java:158) 在java.io.BufferedWriter.flush(BufferedWriter.java:124) 在 org.jivesoftware.smack.util.ObservableWriter.flush(ObservableWriter.java:44) 在 org.jivesoftware.smack.tcp.PacketWriter.writePackets(PacketWriter.java:190) 在 org.jivesoftware.smack.tcp.PacketWriter.access $ 000(PacketWriter.java:40) 在org.jivesoftware.smack.tcp.PacketWriter $ 1.run(PacketWriter.java:77) 引起:libcore.io.ErrnoException:sendto failed:EPIPE(Broken 管) at libcore.io.Posix.sendtoBytes(Native Method) 在libcore.io.Posix.sendto(Posix.java:151) 在libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177) 在libcore.io.IoBridge.sendto(IoBridge.java:473) 在java.net.PlainSocketImpl.write(PlainSocketImpl.java:507) 在java.net.PlainSocketImpl.access $ 100(PlainSocketImpl.java:46) 在 java.net.PlainSocketImpl $ PlainSocketOutputStream.write(PlainSocketImpl.java:269) at java.io.OutputStreamWriter.flushBytes(OutputStreamWriter.java:167) 在java.io.OutputStreamWriter.flush(OutputStreamWriter.java:158) 在java.io.BufferedWriter.flush(BufferedWriter.java:124) 在 org.jivesoftware.smack.util.ObservableWriter.flush(ObservableWriter.java:44) 在 org.jivesoftware.smack.tcp.PacketWriter.writePackets(PacketWriter.java:190) 在 org.jivesoftware.smack.tcp.PacketWriter.access $ 000(PacketWriter.java:40) 在 org.jivesoftware.smack.tcp.PacketWriter $ 1.run(PacketWriter.java:77)
我认为这可能是服务器端的一些问题。任何线索都会受到赞赏。