import java.net.URL;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.jx.lcs.common.huanxin.Constants;
import com.jx.lcs.common.huanxin.HTTPMethod;
import com.jx.lcs.common.huanxin.Roles;
import com.jx.lcs.common.huanxin.httpclient.HTTPClientUtils;
import com.jx.lcs.common.huanxin.vo.ClientSecretCredential;
import com.jx.lcs.common.huanxin.vo.Credential;
import com.jx.lcs.common.huanxin.vo.EndPoints;
/**
* REST API Demo: 发送消息 REST API HttpClient4.3实现
*/
public class EasemobMessages {
private static Logger LOGGER = LoggerFactory.getLogger(EasemobMessages.class);
private static final String APPKEY = Constants.APPKEY;
private static JsonNodeFactory factory = new JsonNodeFactory(false);
// 通过app的client_id和client_secret来获取app管理员token
private static Credential credential = new ClientSecretCredential(Constants.APP_CLIENT_ID,
Constants.APP_CLIENT_SECRET, Roles.USER_ROLE_APPADMIN);
//发送消息到单个用户
public static void sendMsgToUser(String fromUid, String toUid, String content) {
// 给用户发一条文本消息
String from = fromUid;
String targetTypeus = "users";
ObjectNode ext = factory.objectNode();
ArrayNode targetusers = factory.arrayNode();
targetusers.add(toUid);
ObjectNode txtmsg = factory.objectNode();
txtmsg.put("msg", content);
txtmsg.put("type","txt");
ObjectNode sendTxtMessageusernode = sendMessages(targetTypeus, targetusers, txtmsg, from, ext);
if (null != sendTxtMessageusernode) {
LOGGER.info("给" + toUid + "发一条文本消息: " + sendTxtMessageusernode.toString());
}
}
public static void main(String[] args) {
/**
* 注册IM用户[单个]
*/
ObjectNode datanode = JsonNodeFactory.instance.objectNode();
datanode.put("username","test");
datanode.put("password", Constants.DEFAULT_PASSWORD);
ObjectNode createNewIMUserSingleNode = createNewIMUserSingle(datanode);
if (null != createNewIMUserSingleNode) {
LOGGER.info("注册IM用户[单个]: " + createNewIMUserSingleNode.toString());
}
// 检测用户是否在线
// String targetuserPrimaryKey = "683697";
// ObjectNode usernode = getUserStatus(targetuserPrimaryKey);
// if (null != usernode) {
// LOGGER.info("检测用户是否在线: " + usernode.toString());
// }
// 给用户发一条文本消息
/*
String from =