我尝试使用XMPP Smack Api(4.1.4)创建多用户聊天。我已经使用
建立了登录连接XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
configBuilder.setResource(RESOURCE);
configBuilder.setServiceName(DOMAIN);
configBuilder.setHost(HOST);
XMPPTCPConnection connection = new XMPPTCPConnection(configBuilder.build());
connection.addConnectionListener(connectionListener);
AsyncTask<Void, Void, Boolean> connectionThread = new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... arg0) {
try {
connection.connect();
if(connection != null) {
PingManager mPingManager = PingManager.getInstanceFor(connection);
mPingManager.setDefaultPingInterval(1000 * 50);
mPingManager.setPingInterval(1000 * 50);
connection.setPacketReplyTimeout(1000 * 50);
mPingManager.registerPingFailedListener(new PingFailedListener() {
@Override
public void pingFailed() {
// TODO Auto-generated method stub
Log.e("PING", "ping failed");
// AGain Reconnecting code here..
}
});
}
} catch (IOException | SmackException | XMPPException e) {
Log.d(LOCAL_TAG, "connectConnection exc: "+e.getMessage());
}
return null;
}
};
connectionThread.execute();
它成功连接。连接后,我尝试使用
创建群聊 try {
SharedPreferences mPreferences = context.getSharedPreferences("App_Pref", Context.MODE_PRIVATE);
String userJid = userJidAddress(with local ip);
String userName = "Test5";
Log.d(LOCAL_TAG, "createGroupChat jid: "+userJid+" -- connection.getServiceName(): "+connection.getServiceName());
MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);
MultiUserChat muc = manager.getMultiUserChat(userJid);
muc.create("InstantRoom");
Log.d(LOCAL_TAG, "createGroupChat -- Group CEATED Successfully ");
Form form = muc.getConfigurationForm();
Form submitForm = form.createAnswerForm();
List<FormField> fields = form.getFields();
Log.d(LOCAL_TAG, "createGroupChat -- fields.size(): "+fields.size());
for (int i = 0; i < fields.size(); i++) {
FormField field = (FormField) fields.get(i);
if (!FormField.Type.hidden.equals(field.getType()) && field.getVariable() != null) {
submitForm.setDefaultAnswer(field.getVariable());
}
}
List owners = new ArrayList();
owners.add("test5"+connection.getServiceName());
owners.add("test7"); //Another user
submitForm.setAnswer("muc#roomconfig_roomowners", owners);
submitForm.setAnswer("muc#roomconfig_persistentroom", true);
muc.sendConfigurationForm(submitForm);
muc.join("InstantRoom");
}
catch(Exception e){
Log.d(LOCAL_TAG, "createGroupChat -- Exception: "+e.toString());
}
但是在创建组时,我得到了一个例外
"org.jivesoftware.smack.SmackException$NoResponseException: No response received within reply timeout. Timeout was 50000ms (~50s). Used filter: AndFilter: (FromMatchesFilter (full): test5@172.21.4.199/instantroom, StanzaTypeFilter: org.jivesoftware.smack.packet.Presence)."
如果我增加PingIntervals和PacketReplyTimeout毫秒,那么我收到一条消息
&#34;线程[5,tid = 4403,WaitingInMainSignalCatcherLoop,Thread * = 0xb8e933a8,peer = 0x12c000a0,&#34; Signal Catcher&#34;]:对信号3&#34;做出反应。
没有该异常,应用程序设置为强制自动关闭。希望如此,我在muc.create(&#34; InstantRoom&#34;)行获得例外,因为我无法在该行之后获取日志。 任何人都可以通过弄清楚我做错了什么以及为什么我会收到此错误以及如何在此之后继续操作来帮助我。提前谢谢。
答案 0 :(得分:1)
这两行是错误的
MultiUserChat muc = manager.getMultiUserChat(userJid);
muc.create("InstantRoom");
当你获得MUC时,你必须得到一个NAME(所以经理可以告诉你是否已经存在)。
实际上,当您创建muc时,您必须输入用户名。
所以
String myMUCName = "InstantRoom";
String myMUCService = "conference.myServer";
String myMUCfullName = myMUCName + "@" + myMUCService;
MultiUserChat muc = manager.getMultiUserChat(myMUCfullName);
muc.create(userName);
(如果这不能解决您的问题,请在确认自己已经在服务器上注册的服务后告诉我 - 也许您在表单上有额外的问题) < / p>