Impl getContacts

This commit is contained in:
Changhua 2023-03-19 11:06:22 +08:00
parent 80d9de2d15
commit 45c029ed79
2 changed files with 42 additions and 12 deletions

View File

@ -3,6 +3,8 @@ package com.iamteer;
import com.iamteer.Wcf.Functions; import com.iamteer.Wcf.Functions;
import com.iamteer.Wcf.Request; import com.iamteer.Wcf.Request;
import com.iamteer.Wcf.Response; import com.iamteer.Wcf.Response;
import com.iamteer.Wcf.RpcContact;
import io.sisu.nng.Socket; import io.sisu.nng.Socket;
import io.sisu.nng.pair.Pair1Socket; import io.sisu.nng.pair.Pair1Socket;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -11,6 +13,7 @@ import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
public class Client { public class Client {
@ -36,6 +39,19 @@ public class Client {
} }
} }
private Response sendCmd(Request req) {
try {
ByteBuffer bb = ByteBuffer.wrap(req.toByteArray());
socket.send(bb);
ByteBuffer ret = ByteBuffer.allocate(BUFFER_SIZE);
long size = socket.receive(ret, true);
return Response.parseFrom(Arrays.copyOfRange(ret.array(), 0, (int) size));
} catch (Exception e) {
logger.error("命令调用失败: ", e);
return null;
}
}
public boolean isLogin() { public boolean isLogin() {
Request req = new Request.Builder().setFuncValue(Functions.FUNC_IS_LOGIN_VALUE).build(); Request req = new Request.Builder().setFuncValue(Functions.FUNC_IS_LOGIN_VALUE).build();
Response rsp = sendCmd(req); Response rsp = sendCmd(req);
@ -58,8 +74,6 @@ public class Client {
public Map<Integer, String> getMsgTypes() { public Map<Integer, String> getMsgTypes() {
Request req = new Request.Builder().setFuncValue(Functions.FUNC_GET_MSG_TYPES_VALUE).build(); Request req = new Request.Builder().setFuncValue(Functions.FUNC_GET_MSG_TYPES_VALUE).build();
Response rsp = sendCmd(req); Response rsp = sendCmd(req);
Map<Integer, String> types = new HashMap<>();
if (rsp != null) { if (rsp != null) {
return rsp.getTypes().getTypesMap(); return rsp.getTypes().getTypesMap();
} }
@ -67,6 +81,16 @@ public class Client {
return Wcf.MsgTypes.newBuilder().build().getTypesMap(); return Wcf.MsgTypes.newBuilder().build().getTypesMap();
} }
public List<RpcContact> getContacts() {
Request req = new Request.Builder().setFuncValue(Functions.FUNC_GET_CONTACTS_VALUE).build();
Response rsp = sendCmd(req);
if (rsp != null) {
return rsp.getContacts().getContactsList();
}
return Wcf.RpcContacts.newBuilder().build().getContactsList();
}
public void waitMs(int ms) { public void waitMs(int ms) {
try { try {
Thread.sleep(ms); Thread.sleep(ms);
@ -75,16 +99,21 @@ public class Client {
} }
} }
private Response sendCmd(Request req) { public void printContacts(List<RpcContact> contacts) {
try { for (RpcContact c : contacts) {
ByteBuffer bb = ByteBuffer.wrap(req.toByteArray()); // logger.info(c.getWxid());
socket.send(bb); int value = c.getGender();
ByteBuffer ret = ByteBuffer.allocate(BUFFER_SIZE); String gender;
long size = socket.receive(ret, true); if (value == 1) {
return Response.parseFrom(Arrays.copyOfRange(ret.array(), 0, (int) size)); gender = "";
} catch (Exception e) { } else if (value == 2) {
logger.error("命令调用失败: ", e); gender = "";
return null; } else {
gender = "未知";
}
logger.info("{}, {}, {}, {}, {}, {}, {}", c.getWxid(), c.getName(), c.getCode(), c.getCountry(),
c.getProvince(), c.getCity(), gender);
} }
} }
} }

View File

@ -13,5 +13,6 @@ public class Main {
logger.info("isLogin: {}", client.isLogin()); logger.info("isLogin: {}", client.isLogin());
logger.info("wxid: {}", client.getSelfWxid()); logger.info("wxid: {}", client.getSelfWxid());
logger.info("message types: {}", client.getMsgTypes()); logger.info("message types: {}", client.getMsgTypes());
client.printContacts(client.getContacts());
} }
} }