Skip navigation
This discussion is archived
31136 Views 20 Replies Latest reply: Jun 8, 2007 4:20 AM by chase RSS
Calculating status... 5 posts since
Sep 27, 2006
Currently Being Moderated

May 22, 2007 5:14 PM

Example for GoogleTalk with Smack 3.0.x

Hi,

 

I had smack 2.2.1 (somehow) working with Google Talk.

 

I''ve tried to upgrade but I now get an IllegalStateException...

 

This is my code (modified ''guessed'' for 3.0.2)

XMPPConnection gtalk = null;

 

ConnectionConfiguration conf = new ConnectionConfiguration("talk.google.com", 5222, "googlemail.com");

 

gtalk = new XMPPConnection(conf);

gtalk.login("myGoogleIdWithout@", "myPassword", "smack");

 

Chat chat = gtalk.getChatManager().getThreadChat("myfriend@googlemail.com");

chat.sendMessage("HI Dude");

 

I get this:

java.lang.IllegalStateException: Not connected to server.

     at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:329)

     at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:301)

 

Could somebody post a working version end-2-end to simply: connect (googlemail please), create a chat and send a message to someone...

 

Many thanks!!!!

 

Benoit.

  • chase Bronze 44 posts since
    May 25, 2007
    Currently Being Moderated
    May 25, 2007 3:59 AM (in response to benoitx)
    Re: Example for GoogleTalk with Smack 3.0.x

    From the XMMPConnection Javadocs...

     

    Note that XMPPConnection constructors do not establish a connection to the server and you must call connect()

     

    Call connect() before login

     

  • chase Bronze 44 posts since
    May 25, 2007
    Currently Being Moderated
    May 25, 2007 9:57 PM (in response to benoitx)
    Re: Example for GoogleTalk with Smack 3.0.x

    Here''s a working GTalk example I wrote with Smack 3.0.2

     

    
    import java.io.IOException;
    import org.jivesoftware.smack.Chat;
    import org.jivesoftware.smack.ChatManager;
    import org.jivesoftware.smack.ConnectionConfiguration;
    import org.jivesoftware.smack.MessageListener;
    import org.jivesoftware.smack.XMPPConnection;
    import org.jivesoftware.smack.XMPPException;
    import org.jivesoftware.smack.packet.Message;
    import org.jivesoftware.smack.packet.Presence;
    
    public class SendTest {
        
        public static class MessageParrot implements MessageListener {
            
            private Message msg = new Message("macro10.com@gmail.com", Message.Type.chat);
            
            // gtalk seems to refuse non-chat messages
            // messages without bodies seem to be caused by things like typing
            public void processMessage(Chat chat, Message message) {
                if(message.getType().equals(Message.Type.chat) && message.getBody() != null) {
                    System.out.println("Received: " + message.getBody());
                    try {
                        msg.setBody("I am a Java bot. You said: " + message.getBody());
                        chat.sendMessage(msg);
                    } catch (XMPPException ex) {
                        //ex.printStackTrace();
                        System.out.println("Failed to send message");
                    }
                } else {
                    System.out.println("I got a message I didn''t understand");
                }
            }
        }
        
        
        public static void main( String[] args ) {
            
            System.out.println("Starting IM client");
            
            // gtalk requires this or your messages bounce back as errors
            ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
            XMPPConnection connection = new XMPPConnection(connConfig);
            
            try {
                connection.connect();
                System.out.println("Connected to " + connection.getHost());
            } catch (XMPPException ex) {
                //ex.printStackTrace();
                System.out.println("Failed to connect to " + connection.getHost());
                System.exit(1);
            }
            try {
                connection.login("USERNAME", "PASSWORD");
                System.out.println("Logged in as " + connection.getUser());
                
                Presence presence = new Presence(Presence.Type.available);
                connection.sendPacket(presence);
                
            } catch (XMPPException ex) {
                //ex.printStackTrace();
                System.out.println("Failed to log in as " + connection.getUser());
                System.exit(1);
            }
            
            ChatManager chatmanager = connection.getChatManager();
            Chat chat = chatmanager.createChat("macro10.com@gmail.com", new MessageParrot());
            
            try {
                // google bounces back the default message types, you must use chat
                Message msg = new Message("macro10.com@gmail.com", Message.Type.chat);
                msg.setBody("Test");
                chat.sendMessage(msg);
            } catch (XMPPException e) {
                System.out.println("Failed to send message");
                // handle this how?
            }
            
            System.out.println("Press enter to disconnect");
            
            try {
                System.in.read();
            } catch (IOException ex) {
                //ex.printStackTrace();
            }
            
            connection.disconnect();  
        }
    }
    

     

    • Calculating status... 1 posts since
      Jun 3, 2007
      Currently Being Moderated
      Jun 3, 2007 5:48 PM (in response to chase)
      Re: Example for GoogleTalk with Smack 3.0.x

      Hi,

       

      Thanks for the sample codes. I changed the login name and password and managed to run it in my Eclipse 3.2 with JDK 1.5.x, but it returned this error:

       

      Starting IM client

      Connected to talk.google.com

      Failed to log in as null

       

      Why is there a "null" there? Did I missed to replace something else?

       

      Please help.

       

      Regards,

      Sylver

  • Calculating status... 2 posts since
    May 4, 2007
    Currently Being Moderated
    Jun 4, 2007 10:54 AM (in response to benoitx)
    Re: Example for GoogleTalk with Smack 3.0.x

    hi,

    i have also the same problem with the null.

    when i examine the exception in debug i found out that the null refers to the fact that the value for the server is null.

    The question is which server? mine which cannot be resolved?

    • Calculating status... 5 posts since
      Jun 4, 2007
      Currently Being Moderated
      Jun 4, 2007 2:03 PM (in response to em111)
      Re: Example for GoogleTalk with Smack 3.0.x

      I am having exactly the same problems! have spend ages trying to figure it out but can''t see what''s wrong?

      • chase Bronze 44 posts since
        May 25, 2007
        Currently Being Moderated
        Jun 4, 2007 3:40 PM (in response to Joel Selvadurai)
        Re: Example for GoogleTalk with Smack 3.0.x

        The example I gave really does work, I promise. The null was simply because the XMPPConnection does something I don''t think it should, it only stores the username for successful logins. I think it''s a bug in Smack but it''s one of those kind of things that the Smack developers might call a feature.

         

        After using Smack for a little while I''ve found the Chat class to be kind of worthless so I''ve modified the example to avoid using it. I''ve also created a junk gtalk account so I could leave the username and password values in the example. If you had a problem with the last example it''s probably because you were using username@gmail.com instead of just username. Run this example with

         

        java SendTest

         

        to just wait for messages and auto-reply to them. Run with

         

        java SendTest another.user@gmail.com

         

        to do the same but also send a single message to start the conversation.

         

        This example runs with zero modifications but remember if you don''t change the username and password values then everyone running the example is going to be logged in at the same time and I think only one of you is going to get the messages.

         

        
        import java.io.IOException;
        import org.jivesoftware.smack.ConnectionConfiguration;
        import org.jivesoftware.smack.PacketListener;
        import org.jivesoftware.smack.XMPPConnection;
        import org.jivesoftware.smack.XMPPException;
        import org.jivesoftware.smack.filter.MessageTypeFilter;
        import org.jivesoftware.smack.filter.PacketFilter;
        import org.jivesoftware.smack.packet.Message;
        import org.jivesoftware.smack.packet.Packet;
        import org.jivesoftware.smack.packet.Presence;
        import org.jivesoftware.smack.util.StringUtils;
        
        public class SendTest {
            
            // Notice the username is NOT smack.test@gmail.com
            private static String username = "smack.test";
            private static String password = "ignite";
            
            public static class MessageParrot implements PacketListener {
                private XMPPConnection xmppConnection;
                
                public MessageParrot(XMPPConnection conn) {
                    xmppConnection = conn;
                }
                
                public void processPacket(Packet packet) {
                    Message message = (Message)packet;
                    if(message.getBody() != null) {
                        String fromName = StringUtils.parseBareAddress(message.getFrom());
                        System.out.println("Message from " + fromName + "\n" + message.getBody() + "\n");
                        Message reply = new Message();
                        reply.setTo(fromName);
                        reply.setBody("I am a Java bot. You said: " + message.getBody());
                        xmppConnection.sendPacket(reply);
                    }
                }
            };
            
            
            public static void main( String[] args ) {
                
                System.out.println("Starting IM client");
                
                // gtalk requires this or your messages bounce back as errors
                ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
                XMPPConnection connection = new XMPPConnection(connConfig);
                
                try {
                    connection.connect();
                    System.out.println("Connected to " + connection.getHost());
                } catch (XMPPException ex) {
                    //ex.printStackTrace();
                    System.out.println("Failed to connect to " + connection.getHost());
                    System.exit(1);
                }
                try {
                    connection.login(username, password);
                    System.out.println("Logged in as " + connection.getUser());
                    
                    Presence presence = new Presence(Presence.Type.available);
                    connection.sendPacket(presence);
                    
                } catch (XMPPException ex) {
                    //ex.printStackTrace();
                    // XMPPConnection only remember the username if login is succesful
                    // so we can''t use connection.getUser() unless we log in correctly
                    System.out.println("Failed to log in as " + username);
                    System.exit(1);
                }
                
                PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
                connection.addPacketListener(new MessageParrot(connection), filter);
                
                if(args.length > 0) {
                    // google bounces back the default message types, you must use chat
                    Message msg = new Message(args[0], Message.Type.chat);
                    msg.setBody("Test");
                    connection.sendPacket(msg);
                }
                
                System.out.println("Press enter to disconnect\n");
                
                try {
                    System.in.read();
                } catch (IOException ex) {
                    //ex.printStackTrace();
                }
                
                connection.disconnect();
            }
        }
        

         

        • Calculating status... 5 posts since
          Jun 4, 2007
          Currently Being Moderated
          Jun 4, 2007 4:08 PM (in response to chase)
          Re: Example for GoogleTalk with Smack 3.0.x

          This example still doesn''t work for me? I get the following outcome:

           

          Starting IM client

          Connected to talk.google.com

          Failed to log in as smack.test

          Java Result: 1

           

          And I get the same outcome when I connect with my own gmail account?

           

          I am in London.. is it something to do with having a connection in europe? This is really strange.. I am using a mac and java version:

          Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-164)

          • Calculating status... 5 posts since
            Jun 4, 2007
            Currently Being Moderated
            Jun 4, 2007 4:13 PM (in response to Joel Selvadurai)
            Re: Example for GoogleTalk with Smack 3.0.x

            By the way, I am using the latest available version of smack api, 3.0.3

            • chase Bronze 44 posts since
              May 25, 2007
              Currently Being Moderated
              Jun 4, 2007 7:13 PM (in response to Joel Selvadurai)
              Re: Example for GoogleTalk with Smack 3.0.x

              I''m using Smack 3.0.2, Windows XP, and JDK 1.6 but it might be a location problem. I''ve only tried at locations within the United States. I think in the UK and in Germany there was an issue with the GMail.com domain what about if you try

               

              ConnectionConfiguration conf = new ConnectionConfiguration("talk.google.com", 5222, "googlemail.com");

               

              • Calculating status... 5 posts since
                Jun 4, 2007
                Currently Being Moderated
                Jun 5, 2007 2:04 AM (in response to chase)
                Re: Example for GoogleTalk with Smack 3.0.x

                hmm.. still not working? Maybe it''s the version of java for mac that I have? Is it working for everyone else now?

                • chase Bronze 44 posts since
                  May 25, 2007
                  Currently Being Moderated
                  Jun 5, 2007 4:47 AM (in response to Joel Selvadurai)
                  Re: Example for GoogleTalk with Smack 3.0.x

                  I doubt it''s your Java as long as it runs and compiles. You tried with the googlemail.com domain and an account created in the UK? I''d suggest creating a jabber.org account and modifying the connection info, username, and password just to make sure this is a server issue and nothing with your environment. Once that''s working then use a IM client like pidgin to figure out the settings you need in the UK for gtalk. If anyone has a UK computer that I can have remote access to (RDP/VNC) with Java I''d be more than happy to work on this problem but there doesn''t seem to be anything else I can do from my current location.

                  • Calculating status... 5 posts since
                    Jun 4, 2007
                    Currently Being Moderated
                    Jun 5, 2007 3:14 PM (in response to chase)
                    Re: Example for GoogleTalk with Smack 3.0.x

                    If I use:

                     

                    ConnectionConfiguration connConfig = new ConnectionConfiguration("jabber.org", 5222, "jabber.org");

                     

                    and use my jabber.org username and password, it connects fine! Shall I email you the ssh details of my host and you can try to see if you can get it working on there?

                • Calculating status... 1 posts since
                  Jun 8, 2007
                  Currently Being Moderated
                  Jun 8, 2007 1:43 AM (in response to Joel Selvadurai)
                  Re: Example for GoogleTalk with Smack 3.0.x

                  I encountered the same problem:

                   

                  Starting IM client

                  Connected to talk.google.com

                  Failed to log in as smack.test

                  it happened both on my own account and the account given

                   

                  I am in canada and I use JAVA 1.6 + SMACK 3.0.2

                   

                  Chase, Can you also explain why there no SASL authentication in your code? seems like googe talk requries SASL?

                   

                  Cheers,

                  Cooler

                  • chase Bronze 44 posts since
                    May 25, 2007
                    Currently Being Moderated
                    Jun 8, 2007 4:20 AM (in response to cooler813)
                    Re: Example for GoogleTalk with Smack 3.0.x

                    It should work with 3.0.2. There is no SASL or other security code because "maximum security will be used when connecting to the server by default (and when possible), including use of TLS encryption" according to the docs that come with Smack. It''s in the smack_3_0_2/documentation/gettingstarted.html page. Try turning on the debugger by adding a new line to your main method:

                    public static void main( String[] args ) {
                            XMPPConnection.DEBUG_ENABLED = true;
                            .....
                    }
                    

                     

  • Calculating status... 1 posts since
    Jun 6, 2007
    Currently Being Moderated
    Jun 6, 2007 8:21 PM (in response to benoitx)
    Re: Example for GoogleTalk with Smack 3.0.x

    I''m getting the SASL authentication failed errors as the others reported when using 3.0.3.  Everything works fine when using the 2.2.1 libraries.

     

    It seems the code posted doesn''t work.  Anybody have 3.0.3 working code?

     

    ConnectionConfiguration config = new ConnectionConfiguration(

                        "talk.google.com", 5222, "gmail.com");

     

    XMPPConnection con = new XMPPConnection(config);

     

    try {

    con.connect();

     

    // username@gmail.com google talk account

    con.login(username, password);

               

    } catch (XMPPException ex) {

                    

    }

    con.disconnect();

More Like This

  • Retrieving data ...

Incoming Links

Bookmarked By (0)