This article focuses on creating rooms and packets.
1. Create a room:
In fact, it is very easy to create a room, just a few words. But you need to know that the server does not help you configure room information or anything at the same time. Therefore, the following method is generally used to create a room.
// Use xmppconnection to create a multiuserchat muc = new multiuserchat (conn1, "myroom@conference.jabber.org"); // create a chat room MUC. create ("testbot"); // send an empty form configuration, which shows that we want an instant room MUC. sendconfigurationform (new form (Form. type_submit ));
So we should manually configure some information to avoid future problems. But here I want to remind my friends that no matter which method is used, it's a shit or a shit. Look at the MUC. It was originally a chat room, and you have to create another chat room in the chat room. Mom has children, and then the children and Mom are side by side. Shit, but I don't know what it means. Hope that the experts can give you some advice.
Try {// create a chat room MUC. create (constants. vCard. getnickname (). tostring (); // obtain the configuration form Form of the chat room. form = MUC. getconfigurationform (); // create a new form to be submitted based on the original form. Form submitform = form. createanswerform (); // Add the default response to the form to be submitted for (iterator fields = form. getfields (); fields. hasnext ();) {formfield field = (formfield) fields. next (); If (! Formfield. type_hidden.equals (field. GetType () & field. getvariable ()! = NULL) {// set the default value to reply to submitform. setdefaultanswer (field. getvariable () ;}}// set the new owner of the chat room // list owners = new arraylist (); // owners. add ("liaonaibo2 \ 40slook. cc "); // owners. add ("liaonaibo1 \ 40slook. cc "); // submitform. setanswer ("MUC # roomconfig_roomowners", owners); // sets the chat room as a persistent chat room, which is about to be saved as submitform. setanswer ("MUC # roomconfig_persistentroom", true); // The room is only available to members. setanswer ("MUC # roomconfig_membersonly", false); // allow the account owner to invite others to submitform. setanswer ("MUC # roomconfig_allowinvites", true); // roles that occupy the real jid of the worker can be found. // submitform. setanswer ("MUC # roomconfig_whois", "anyone"); // log on to the room and submitform. setanswer ("MUC # roomconfig_enablelogging", true); // only registered nicknames are allowed to log on to submitform. setanswer ("X-muc # roomconfig_reservednick", true); // you can modify the nickname submitform. setanswer ("X-muc # roomconfig_canchangenick", false); // allows users to register a room submitform. setanswer ("X-muc # roomconfig_registration", false); // send the completed form (with the default value) to the server to configure the chat room MUC. sendconfigurationform (submitform);} catch (xmppexception e) {e. printstacktrace ();}
There is another detail. Some friends may have seen the \ 40 Code in my comments. The reason is that the unique jid in XMPP does not allow @, so escape is required. OK?
2. Packets
Some friends said what the message is, in fact, I also felt a little redundant at first, but think about people going offline like the QQ group, and the avatar name turns gray. If it is an openfire server, you will be removed when you go offline. Therefore, we need to send a broadcast to the server. The specific process is later. Take a look at this message:
<iq id="902U0-48" type="set"><query xmlns="jabber:iq:private"><storage xmlns="storage:bookmarks"><conference name="ccc" autojoin="true" jid="ccc@conference.xmpp.chaoboo.com" ></conference></storage></query></iq>
You can modify the server plug-in as needed.
The following message is sent:
/*** The message sent to the server when the user joins * @ return */Public IQ joinxml () {IQ = new IQ () {Public String getchildelementxml () {stringbuilder Buf = new stringbuilder (); Buf. append ("<query xmlns = \" jabber: IQ: Private \ ">"); Buf. append ("<storage xmlns = \" Storage: bookmarks \ ">"); Buf. append ("<"). append ("Conference "). append ("name = \" CCC \""). append ("autojoin = \" false \""). append (""); Buf. append ("</storage>"); Buf. append ("</query>"); Return Buf. tostring () ;}}; IQ. settype (IQ. type. set); // method such as name. Here is the jid from which this message is sent, and the/smack behind it is the end of this message, for example, spark is/spark, android is/smackiq. setfrom (constants. username + "@ naibo.liao.com/smack"); Return IQ ;}
How can I send it? It can be sent through connection, that is, xmppconnection:
Constants.conn.sendPacket(leaveXml());
OK. Stop for a while. Your hands are sour.