Skip to content

Commit 493c4e5

Browse files
committed
modify readme.md
1 parent 647b6bb commit 493c4e5

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-1
lines changed

README.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,71 @@ public class TestSimpleProxy implements ApiProxy {
109109
* MessageBuf.JMTransfer: this is important, this class is created by protobuf, it include header and body, header is app information, you can get the detail from this project.
110110
#### Output Parameter:
111111
* MessageWrapper: it is message response wrapper. It include protocol, sessionId and body. Body is response data.
112-
113112
body is byte type, and it also a protobuf bytes.
113+
### Send Notify to Client
114+
```java
115+
private NotifyProxy notify;
116+
117+
final int timeout = 10 * 1000;
118+
final int NOTIFY = 3;
119+
120+
/**
121+
* session
122+
*/
123+
final String VERSION = "version";
124+
final String DEVICE_ID = "deviceId";
125+
final String PLATFORM = "platform";
126+
final String PLATFORM_VERSION = "platformVersion";
127+
final String TOKEN = "token";
128+
final String APP_KEY = "appKey";
129+
final String TIMESTAMP = "timestamp";
130+
final String SIGN = "sign";
131+
132+
/**
133+
* need session into redis, then when you notify you can get info from redis by session
134+
*/
135+
final Map<String, Map<String, Object>> testSessionMap = null;
136+
137+
public boolean send(long seq, String sessionId, int cmd, ByteString body) throws Exception {
138+
boolean success = false;
139+
MessageBuf.JMTransfer.Builder builder = generateNotify(sessionId, seq, cmd, body);
140+
if (builder != null) {
141+
MessageWrapper wrapper = new MessageWrapper(MessageWrapper.MessageProtocol.NOTIFY, sessionId, builder);
142+
int ret = notify.notify(seq, wrapper, timeout);
143+
if (ret == Constants.NOTIFY_SUCCESS) {
144+
success = true;
145+
} else if (ret == Constants.NOTIFY_NO_SESSION) {
146+
/** no session on this machine **/
147+
success = true;
148+
}
149+
} else {
150+
/** no session in the cache **/
151+
success = true;
152+
}
153+
return success;
154+
}
155+
156+
protected MessageBuf.JMTransfer.Builder generateNotify(String sessionId, long seq, int cmd, ByteString body) throws Exception {
157+
Map<String, Object> map = testSessionMap.get(sessionId);
158+
159+
MessageBuf.JMTransfer.Builder builder = MessageBuf.JMTransfer.newBuilder();
160+
builder.setVersion(String.valueOf(map.get(VERSION)));
161+
builder.setDeviceId(String.valueOf(map.get(DEVICE_ID)));
162+
builder.setCmd(cmd);
163+
builder.setSeq(seq);
164+
builder.setFormat(NOTIFY);
165+
builder.setFlag(0);
166+
builder.setPlatform(String.valueOf(map.get(PLATFORM)));
167+
builder.setPlatformVersion(String.valueOf(map.get(PLATFORM_VERSION)));
168+
builder.setToken(String.valueOf(map.get(TOKEN)));
169+
builder.setAppKey(String.valueOf(map.get(APP_KEY)));
170+
builder.setTimeStamp(String.valueOf(map.get(TIMESTAMP)));
171+
builder.setSign(String.valueOf(map.get(SIGN)));
172+
builder.setBody(body);
173+
174+
return builder;
175+
}
176+
```
114177
## Tcp Client
115178
support iOS, android, C++ languages
116179
### Serialize protobuf
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.linkedkeeper.tcp.server;
2+
3+
import com.google.protobuf.ByteString;
4+
import com.linkedkeeper.tcp.connector.tcp.codec.MessageBuf;
5+
import com.linkedkeeper.tcp.constant.Constants;
6+
import com.linkedkeeper.tcp.message.MessageWrapper;
7+
import com.linkedkeeper.tcp.notify.NotifyProxy;
8+
9+
import java.util.Map;
10+
11+
public class TestNotify {
12+
13+
private NotifyProxy notify;
14+
15+
final int timeout = 10 * 1000;
16+
final int NOTIFY = 3;
17+
18+
/**
19+
* session
20+
*/
21+
final String VERSION = "version";
22+
final String DEVICE_ID = "deviceId";
23+
final String PLATFORM = "platform";
24+
final String PLATFORM_VERSION = "platformVersion";
25+
final String TOKEN = "token";
26+
final String APP_KEY = "appKey";
27+
final String TIMESTAMP = "timestamp";
28+
final String SIGN = "sign";
29+
30+
final Map<String, Map<String, Object>> testSessionMap = null;
31+
32+
public boolean send(long seq, String sessionId, int cmd, ByteString body) throws Exception {
33+
boolean success = false;
34+
MessageBuf.JMTransfer.Builder builder = generateNotify(sessionId, seq, cmd, body);
35+
if (builder != null) {
36+
MessageWrapper wrapper = new MessageWrapper(MessageWrapper.MessageProtocol.NOTIFY, sessionId, builder);
37+
int ret = notify.notify(seq, wrapper, timeout);
38+
if (ret == Constants.NOTIFY_SUCCESS) {
39+
success = true;
40+
} else if (ret == Constants.NOTIFY_NO_SESSION) {
41+
/** no session on this machine **/
42+
success = true;
43+
}
44+
} else {
45+
/** no session in the cache **/
46+
success = true;
47+
}
48+
return success;
49+
}
50+
51+
52+
protected MessageBuf.JMTransfer.Builder generateNotify(String sessionId, long seq, int cmd, ByteString body) throws Exception {
53+
Map<String, Object> map = testSessionMap.get(sessionId);
54+
55+
MessageBuf.JMTransfer.Builder builder = MessageBuf.JMTransfer.newBuilder();
56+
builder.setVersion(String.valueOf(map.get(VERSION)));
57+
builder.setDeviceId(String.valueOf(map.get(DEVICE_ID)));
58+
builder.setCmd(cmd);
59+
builder.setSeq(seq);
60+
builder.setFormat(NOTIFY);
61+
builder.setFlag(0);
62+
builder.setPlatform(String.valueOf(map.get(PLATFORM)));
63+
builder.setPlatformVersion(String.valueOf(map.get(PLATFORM_VERSION)));
64+
builder.setToken(String.valueOf(map.get(TOKEN)));
65+
builder.setAppKey(String.valueOf(map.get(APP_KEY)));
66+
builder.setTimeStamp(String.valueOf(map.get(TIMESTAMP)));
67+
builder.setSign(String.valueOf(map.get(SIGN)));
68+
builder.setBody(body);
69+
70+
return builder;
71+
}
72+
73+
}

0 commit comments

Comments
 (0)