Thread: Java class help

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Memphis, TN
    Posts
    199
    Rep Power
    12

    Java class help


    I have to admit I have little experience with java. I mainly program with some of the older languages. I was reading information about setting up a messaging client, and I have no idea what he is talking about in the last step.


    Send your first Jabber message
    To send a Jabber instant message programmatically, you must initialize the Muse Jabber API. Do that by creating an instance of the JabberContext class, then use the context as a parameter to the createSession() method on the Jabber session factory class:

    1 // Initialize the Jabber context
    2 JabberContext jabberContext = new JabberContext("user", "pass", "localhost");
    3
    4 // Create an instance of the Jabber session factory
    5 Jabber jabber = new Jabber();
    6 // Create the new session
    7 JabberSession jabberSession = jabber.createSession(jabberContext);


    The above example shows the creation of a new context at line 2. The JabberContext stores specific user-related information (username, password, server address) and also contains a unique session identifier when the session is later established using the context. For illustration purposes, I have hardcoded the username, password, and server.

    At line 5, a Jabber session factory is created, which we use at line 7 to create a new JabberSession, Muse's main interface into the services offered by the Jabber server. The server's main services are:


    Connection services: For connecting to and disconnecting from the Jabber server
    User service: For user authentication and registration
    Presence service: For receiving presence information from other users/services and broadcasting your own presence
    Roster service: Jabber-speak for a buddy list or address book
    Chat service: Sends messages of various typesógroup chat, private messages, headlines, and so on
    Server service: Obtains information relating to the services offered by this Jabber server
    Client service: Obtains information about other users, such as the last time the user signed in
    Now that we have an initialized Jabber session, we can use it to connect to the Jabber server using the connect() method on the JabberSession object we just created:

    8 // Connect to the server
    9 jabberSession.connect("localhost", 5222);


    To connect to a Jabber server, we specify the address and port number of the machine on which the server is located. The standard, default Jabber port number is 5222 and should rarely change.

    Now that the JabberSession has connected to the server, we can log in with the login() method on the user service:

    10 // Log in to the Jabber server
    11 jabberSession.getUserService().login();


    At line 11, we use the JabberSession to obtain a reference to the UserService, then call the login() method on the user service. Notice that the method itself did not specify any user information. login() obtains this information from the JabberContext associated with the JabberSession when the JabberSession was created at line 7 above.

    Now that we have successfully logged in to the Jabber server, we can start sending and receiving messages. The following code fragment shows how to construct a simple headline-style message:

    12 // Construct test message
    13 JabberChatMessage msg = new
    14 JabberChatMessage(JabberChatMessage.TYPE_HEADLINE);
    15 msg.setSubject("Hello world");
    16 msg.setBody("Hello world");
    17 msg.setTo("user2@localhost");


    At line 13, we create a JabberChatMessage instance. The single parameter specifies the type of message we require: TYPE_HEADLINE. The name of the JabberChatMessage class is a little misleading because, in fact, it might be used to contain any of the four types of messagesónormal, chat, headline, and erroródefined in the Jabber protocol. At line 15, setSubject() and setBody() specify the subject and body texts, respectively. Finally, setTo() sets the JID of the message's receiver at line 17.

    Under the covers, the JabberChatMessage converts all of this information into an internal DOM (Document Object Model) tree so the XML can generate easily when we are ready to send the message to the Jabber server.

    The final step: Send the message using the sendMessage() method:

    18 // Send the message
    19 jabberSession.sendMessage(msg);
    Last edited by Sonic98; December 8th, 2002 at 03:01 AM.
  2. #2
  3. dont click here
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    409
    Rep Power
    12
    looks pretty straight foward to me (ive also be programming in Java for about 4 years now).. can u possibly highlight the lines ur confused about.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Memphis, TN
    Posts
    199
    Rep Power
    12
    Well nevermind now. I have found come classes already compiled. I guess I should have taken java and pay more attention in my C++ classes. The only problem with the applets I found is they are in jar. I might be wrong, but I thought people could only open those applets if they download suns java machine as opposed to the one built into IE. Wouldn't I have it recompile for them to work with IE?

IMN logo majestic logo threadwatch logo seochat tools logo