This repository has been archived by the owner on Apr 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 314
/
MyClient.java
110 lines (91 loc) · 3.7 KB
/
MyClient.java
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.github.yuanrw.im.client.sample;
import com.github.yuanrw.im.client.ImClient;
import com.github.yuanrw.im.client.api.ChatApi;
import com.github.yuanrw.im.client.api.ClientMsgListener;
import com.github.yuanrw.im.client.api.UserApi;
import com.github.yuanrw.im.client.domain.Friend;
import com.github.yuanrw.im.common.domain.UserInfo;
import com.github.yuanrw.im.protobuf.generate.Chat;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.CharsetUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Date: 2019-07-09
* Time: 08:08
*
* @author yrw
*/
public class MyClient {
private static Logger logger = LoggerFactory.getLogger(MyClient.class);
private ChatApi chatApi;
private UserInfo userInfo;
private Map<String, Friend> friendMap;
public MyClient(String connectorHost, Integer connectorPort, String restUrl, String username, String password) {
ImClient imClient = start(connectorHost, connectorPort, restUrl);
chatApi = imClient.chatApi();
UserApi userApi = imClient.userApi();
//login and get a token
userInfo = userApi.login(username, DigestUtils.sha256Hex(password.getBytes(CharsetUtil.UTF_8)));
//get friends list
List<Friend> friends = userApi.friends(userInfo.getToken());
friendMap = friends.stream().collect(Collectors.toMap(Friend::getUserId, f -> f));
System.out.println("Here are my friends!");
for (Friend friend : friends) {
System.out.println(friend.getUserId() + ": " + friend.getUsername());
}
}
private ImClient start(String connectorHost, Integer connectorPort, String restUrl) {
ImClient imClient = new ImClient(connectorHost, connectorPort, restUrl);
imClient.setClientMsgListener(new ClientMsgListener() {
@Override
public void online() {
logger.info("[client] i have connected to server!");
}
@Override
public void read(Chat.ChatMsg chatMsg) {
//when it's confirmed that user has read this msg
System.out.println(friendMap.get(chatMsg.getFromId()).getUsername() + ": "
+ chatMsg.getMsgBody().toStringUtf8());
chatApi.confirmRead(chatMsg);
}
@Override
public void hasSent(Long id) {
System.out.println(String.format("msg {%d} has been sent", id));
}
@Override
public void hasDelivered(Long id) {
System.out.println(String.format("msg {%d} has been delivered", id));
}
@Override
public void hasRead(Long id) {
System.out.println(String.format("msg {%d} has been read", id));
}
@Override
public void offline() {
logger.info("[{}] I am offline!", userInfo != null ? userInfo.getUsername() : "client");
}
@Override
public void hasException(ChannelHandlerContext ctx, Throwable cause) {
logger.error("[" + userInfo.getUsername() + "] has error ", cause);
}
});
imClient.start();
return imClient;
}
public void printUserInfo() {
System.out.println("id: " + userInfo.getId());
System.out.println("username: " + userInfo.getUsername());
}
public void send(String id, String text) {
if (!friendMap.containsKey(id)) {
System.out.println("friend " + id + " not found!");
return;
}
chatApi.text(id, text);
}
}