1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.util.Hashtable; import java.util.Iterator; import org.jivesoftware.smack.*; import org.jivesoftware.smack.packet.Message; public class ChatBot { public static void main (String args[]){ } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import java.util.Hashtable; import java.util.Iterator; import org.jivesoftware.smack.*; import org.jivesoftware.smack.packet.Message; public class ChatBot implements Runnable { public static void main (String args[]){ ChatBot test = new ChatBot(); test.go(); } public void run() { Thread current = Thread.currentThread(); while (botThread == current){ try { Thread.sleep(2000); } catch (InterruptedException e){ //Do nothing, this is just the program ending. } } } Thread botThread; String queue = "start"; public void go(){ if (botThread == null) { botThread = new Thread(this, "IMThread"); botThread.start(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | .... public class ChatBot implements Runnable { public static void main (String args[]){ MessageTester test = new MessageTester(); test.go(); test.startNew(); } public void stop(){ conn.disconnect(); } .... XMPPConnection conn; public void startNew(){ try{ conn = new XMPPConnection("myjabberserver.net"); conn.connect(); conn.login("myprimaryaccount", "mypassword"); } catch (Exception e){ System.out.println("StartNew Exception"); e.printStackTrace(); } System.out.println("Done."); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | .... public class ChatBot implements Runnable { Hashtable<String, String> chatQueues = new Hashtable<String, String>(); // Holds the actual queues Hashtable<String, Chat> chatSessions = new Hashtable<String, Chat>(); // Holds the Chat objects .... XMPPConnection conn; private void createConversation(String target){ try { chatQueues.put(target, ""); Chat chat = conn.getChatManager().createChat(target, new MessageListener() { public void processMessage(Chat chat, Message message) {} } ); chatSessions.put(target, chat); } catch (Exception e){ System.out.println("Create Conversation Exception"); e.printStackTrace(); } } public void startNew(){ try{ conn = new XMPPConnection("myjabberserver.net"); conn.connect(); conn.login("myprimaryaccount", "mypassword"); createConversation("myfriend@myjabberserver.net"); } catch (Exception e){ System.out.println("StartNew Exception"); e.printStackTrace(); } System.out.println("Done."); } } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |