创建基于 Ajax 的 IM 客户端(4)基础 bot
 
- UID
- 1066743
|

创建基于 Ajax 的 IM 客户端(4)基础 bot
基础 bot现在,可以开始着手进行构建了。整个系统的核心是 “bot”,一个行为类似用户的 Java 类。它可以登录服务器、发送消息和接收消息。让我们先来创建 bot。
创建 botbot 本身是一个简单的 Java 类,具有若干导入的类和包以供日后使用。创建一个新文件,将其命名为 ChatBot.java(参见清单 1):
清单 1. 基础 bot1
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[]){
}
}
|
Hashtable 和 Iterator 类用来跟踪会话。使用 org.jivesoftware.smack 包与 Jabber 服务器进行交互。
在线程内运行 bot此 bot 需要连续运行,所以需要在线程内运行它(参见清单 2):
清单 2. 在线程内运行 bot1
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();
}
}
}
|
首先,需要为所有线程中的应用程序实现 Runnable 接口。在创建了新对象之后,运行 go() 方法来创建和启动线程本身,此线程又会启动 run() 方法。
现在让我们连接到服务器。
连接到 Jabber接下来,需要使用之前创建的帐号连接到 Jabber 服务器(参见清单 3):
清单 3. 连接到 Jabber 服务器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.");
}
}
|
一旦创建了线程,就可以继续并登录到 Jabber 服务器。由于将来需要访问该连接,所以需要让 XMPPConnection 对象成为一个全局变量。
当然,建立了连接,还要断开连接;如果停止线程,需要使用线程的内置 stop() 方法来断开连接。
创建 Chat接下来,需要创建谈话本身。每个谈话都会发生在 Chat 对象的上下文。Chat 可发送消息到另一个人,而且还包含了响应对方所说的某些内容的功能。
最后,还需要跟踪多个 Chat 对象以及和其进行的交谈,但目前,我们只开启一个谈话(参见清单 4):
清单 4. 创建单个谈话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.");
}
}
|
从底部开始,当创建对象、登录之后,就需要创建新谈话,并提供给它您与之交流的那个人的用户名。
谈话的创建包含两类动作:与 Chat 本身相关的动作和与应用程序相关的动作。就 Chat 本身而言,需要用两条信息创建它。第一个是您想要与之交流的帐号的用户名。第二个是 MessageListener 对象,该对象能告知 Chat 在从帐户收到信息时该如何做。
所幸的是,可以内联创建 MessageListener。在本例中,将 processMessage() 这一相关方法留为空白;稍候再做处理。 |
|
|
|
|
|