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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
import
java.util.Objects;
import
org.jivesoftware.smack.AbstractXMPPConnection;
import
org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import
org.jivesoftware.smack.chat.Chat;
import
org.jivesoftware.smack.chat.ChatManager;
import
org.jivesoftware.smack.chat.ChatManagerListener;
import
org.jivesoftware.smack.chat.ChatMessageListener;
import
org.jivesoftware.smack.packet.Message;
import
org.jivesoftware.smack.tcp.XMPPTCPConnection;
import
org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
public
class
SmackClient {
public
static
void
main(String[] args) {
try
{
String serviceName =
"XXXXXX"
;
String host =
"192.168.0.1"
;
int
port =
5222
;
String myUsername =
"user1"
;
String myPassword =
"123456"
;
String friendUsername =
"user2"
;
XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
builder.setUsernameAndPassword(myUsername, myPassword);
builder.setServiceName(serviceName);
builder.setHost(host);
builder.setPort(port);
builder.setSecurityMode(SecurityMode.disabled);
builder.setDebuggerEnabled(
false
);
XMPPTCPConnectionConfiguration config = builder.build();
AbstractXMPPConnection c =
new
XMPPTCPConnection(config);
c.connect();
c.login();
System.out.println(
"Authenticated = "
+ c.isAuthenticated());
ChatManager chatmanager = ChatManager.getInstanceFor(c);
Chat newChat = chatmanager.createChat(friendUsername +
"@"
+ serviceName);
final
ChatMessageListener messageListener =
new
ChatMessageListener() {
@Override
public
void
processMessage(Chat arg0, Message message) {
String messageBody = message.getBody();
if
(Objects.nonNull(messageBody)) {
try
{
arg0.sendMessage(
"你刚说的是:"
+ messageBody);
}
catch
(Exception e) {
}
}
}
};
ChatManagerListener chatManagerListener =
new
ChatManagerListener() {
@Override
public
void
chatCreated(Chat chat,
boolean
arg1) {
chat.addMessageListener(messageListener);
}
};
chatmanager.addChatListener(chatManagerListener);
try
{
newChat.sendMessage(
"我来咯"
);
}
catch
(Exception e) {
System.out.println(
"Error Delivering block"
);
}
while
(
true
)
;
}
catch
(Exception e) {
e.printStackTrace();
}
}
}
|
附件为所需要的所有类库Jar包。
附件:http://down.51cto.com/data/2368129
本文转自 zl1030 51CTO博客,原文链接:http://blog.51cto.com/zl1030/1846938