极光IM demo Java服务器端工具类 多端在线即时聊天
2018-11-18
阅读 {{counts.readCount}}
评论 {{counts.commentCount}}
极光IM是用于 iOS / Android / web / PC 等多端在线即时聊天的第三方接口,目前在一定额度内可以免费实现其一部分功能。来减少服务端自己开发的成本,提高开发效率。基本功能包含用户注册登录,个人信息设置,单聊,群聊等。本篇内容主要说下java作为服务端来实现的一些基础功能的demo,内容大部分都是复制和修改了官方文档里的例子。
极光IM官方简介:
极光 IM(英文名 JMessage)致力于帮助 App 解决应用内聊天和跨应用聊天问题,开发者可集成SDK,快速实现稳定可靠的聊天功能。目前提供 iOS / Android / web / PC(Windows、macOS) 的 SDK,以及 Rest API 和后台管理系统,满足开发者不同场景下的需求,大大减少开发成本,提高效率。
极光开发者服务SDK采用了模块化的使用模式,即一个核心模块 (JCore)+N 种服务(IM,JPush,JAnalytics)的使用方式,方便开发者同时集成多项极光开发者服务,极大的优化了多模块同时使用时功能模块重复的问题。如下图:
极光IM 官方文档地址:
https://docs.jiguang.cn/jmessage/server/rest_api_im/
http://jpush.github.io/jmessage-api-java-client/apidocs/
https://github.com/jpush/jmessage-api-java-client
引入依赖Maven
- <!-- 极光 -->
- <dependency>
- <groupId>cn.jpush.api</groupId>
- <artifactId>jpush-client</artifactId>
- <version>3.2.15</version>
- </dependency>
- <dependency>
- <groupId>cn.jpush.api</groupId>
- <artifactId>jmessage-client</artifactId>
- <version>1.1.8</version>
- </dependency>
- <dependency>
- <groupId>cn.jpush.api</groupId>
- <artifactId>jiguang-common</artifactId>
- <version>1.1.3</version>
- </dependency>
- <!--<dependency>-->
- <!--<groupId>com.google.code.gson</groupId>-->
- <!--<artifactId>gson</artifactId>-->
- <!--<version>2.3</version>-->
- <!--</dependency>-->
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>1.7.7</version>
- </dependency>
极光IM工具类 , 需要先去极光申请注册极光IM,获取appkey和masterSecret。目前一定额度一下是可以免费使用
- package com.gemini.common.utils;
- import cn.jiguang.common.ServiceHelper;
- import cn.jiguang.common.resp.APIConnectionException;
- import cn.jiguang.common.resp.APIRequestException;
- import cn.jiguang.common.resp.ResponseWrapper;
- import cn.jmessage.api.JMessageClient;
- import cn.jmessage.api.common.model.RegisterInfo;
- import cn.jmessage.api.message.MessageListResult;
- import cn.jmessage.api.user.UserListResult;
- import org.apache.commons.lang.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * 极光im 即时聊天 java服务器端工具类
- * https://docs.jiguang.cn/jmessage/server/rest_api_im/
- * zmh 2018-11-15 14:34:25
- */
- public class JimUtils {
- protected static final Logger LOG = LoggerFactory.getLogger(JimUtils.class);
- // 需要先去极光申请注册极光IM,获取appkey和masterSecret
- private static final String appkey = "";
- private static final String masterSecret = "";
- /** 注册用户 */
- public static Map<String,Object> registerUsers(String username,String password,String nickname) {
- Map<String,Object> result = new HashMap<>();
- JMessageClient client = new JMessageClient(appkey, masterSecret);
- try {
- // username和password的长度限制在 4 ~ 128 位
- if(StringUtils.isBlank(username) || StringUtils.isBlank(password)
- || username.length() < 4 || username.length() > 128
- || password.length() < 4 || password.length() > 128){
- result.put("code",500);
- result.put("message","用户名或密码不合法");
- return result;
- }
- List<RegisterInfo> users = new ArrayList<>();
- RegisterInfo user = RegisterInfo.newBuilder()
- .setUsername(username)
- .setPassword(password)
- .setNickname(nickname)
- .build();
- users.add(user);
- RegisterInfo[] regUsers = new RegisterInfo[users.size()];
- String res = client.registerUsers(users.toArray(regUsers));
- result.put("code",0);
- result.put("message","success");
- LOG.info(res);
- } catch (APIConnectionException e) {
- result.put("code",500);
- result.put("message",e);
- LOG.error("Connection error. Should retry later. ", e);
- } catch (APIRequestException e) {
- result.put("code",600);
- result.put("message",e.getErrorMessage());
- LOG.error("Error response from JPush server. Should review and fix it. ", e);
- LOG.info("HTTP Status: " + e.getStatus());
- LOG.info("Error Message: " + e.getMessage());
- }
- return result;
- }
- /** 获取所有用户列表 */
- public static Map<String,Object> getAllUsers(Integer start,Integer end) {
- Map<String,Object> result = new HashMap<>();
- JMessageClient client = new JMessageClient(appkey, masterSecret);
- try {
- UserListResult userList = client.getUserList(start, end);
- result.put("code",0);
- result.put("userList",userList);
- LOG.info("success");
- } catch (APIConnectionException e) {
- result.put("code",500);
- result.put("message",e);
- LOG.error("Connection error. Should retry later. ", e);
- } catch (APIRequestException e) {
- result.put("code",600);
- result.put("message",e.getErrorMessage());
- LOG.error("Error response from JPush server. Should review and fix it. ", e);
- LOG.info("HTTP Status: " + e.getStatus());
- LOG.info("Error Message: " + e.getMessage());
- }
- return result;
- }
- /** 更新昵称 */
- public static Map<String,Object> changeNickname(String username,String nickname) {
- Map<String,Object> result = new HashMap<>();
- JMessageClient client = new JMessageClient(appkey, masterSecret);
- try {
- if(StringUtils.isBlank(username)
- || username.length() < 4 || username.length() > 128){
- result.put("code",500);
- result.put("message","用户名不合法");
- return result;
- }
- client.updateUserInfo(username,nickname,null,null,0,null,null,null);
- result.put("code",0);
- result.put("message","success");
- LOG.info("success");
- } catch (APIConnectionException e) {
- result.put("code",500);
- result.put("message",e);
- LOG.error("Connection error. Should retry later. ", e);
- } catch (APIRequestException e) {
- result.put("code",600);
- result.put("message",e.getErrorMessage());
- LOG.error("Error response from JPush server. Should review and fix it. ", e);
- LOG.info("HTTP Status: " + e.getStatus());
- LOG.info("Error Message: " + e.getMessage());
- }
- return result;
- }
- /** 删除用户 */
- public static Map<String,Object> deleteUser(String username) {
- Map<String,Object> result = new HashMap<>();
- JMessageClient client = new JMessageClient(appkey, masterSecret);
- try {
- if(StringUtils.isBlank(username)
- || username.length() < 4 || username.length() > 128){
- result.put("code",500);
- result.put("message","用户名不合法");
- return result;
- }
- client.deleteUser(username);
- result.put("code",0);
- result.put("message","success");
- LOG.info("success");
- } catch (APIConnectionException e) {
- result.put("code",500);
- result.put("message",e);
- LOG.error("Connection error. Should retry later. ", e);
- } catch (APIRequestException e) {
- result.put("code",600);
- result.put("message",e.getErrorMessage());
- LOG.error("Error response from JPush server. Should review and fix it. ", e);
- LOG.info("HTTP Status: " + e.getStatus());
- LOG.info("Error Message: " + e.getMessage());
- }
- return result;
- }
- /** 禁用/启用 用户,1启用 0禁用 */
- public static Map<String,Object> forbidUser(String username,Integer disable) {
- Map<String,Object> result = new HashMap<>();
- JMessageClient client = new JMessageClient(appkey, masterSecret);
- try {
- ResponseWrapper res = client.forbidUser(username, disable == 0);
- result.put("code",0);
- result.put("message","success");
- LOG.info("response code: " + res.responseCode);
- } catch (APIConnectionException e) {
- result.put("code",500);
- result.put("message",e);
- LOG.error("Connection error. Should retry later. ", e);
- } catch (APIRequestException e) {
- result.put("code",600);
- result.put("message",e.getErrorMessage());
- LOG.error("Error response from JPush server. Should review and fix it. ", e);
- LOG.info("HTTP Status: " + e.getStatus());
- LOG.info("Error Message: " + e.getMessage());
- }
- return result;
- }
- /** 获取用户聊天记录 时间格式 yyyy-MM-dd HH:mm:ss */
- public static Map<String,Object> getUserMessages(String username,Integer count,String beginTime,String endTime) {
- Map<String,Object> result = new HashMap<>();
- JMessageClient client = new JMessageClient(appkey, masterSecret);
- try {
- MessageListResult userMessages = client.getUserMessages(username, count, beginTime, endTime);
- result.put("code",0);
- result.put("userMessages",userMessages);
- LOG.info("success");
- } catch (APIConnectionException e) {
- result.put("code",500);
- result.put("message",e);
- LOG.error("Connection error. Should retry later. ", e);
- } catch (APIRequestException e) {
- result.put("code",600);
- result.put("message",e.getErrorMessage());
- LOG.error("Error response from JPush server. Should review and fix it. ", e);
- LOG.info("HTTP Status: " + e.getStatus());
- LOG.info("Error Message: " + e.getMessage());
- }
- return result;
- }
- public static void main(String[] args) {
- // System.out.println(registerUsers("username","password","nickname"));
- // System.out.println(getAllUsers(0,1000));
- // System.out.println(changeNickname("username","nickname"));
- // System.out.println(deleteUser("username"));
- // System.out.println(forbidUser("username",0));
- // System.out.println(getUserMessages("username",1000,"2018-01-01 00:00:00","2018-12-31 23:59:59"));
- }
- }
评论区空空如也,赶紧添加一条评论吧
评论 {{counts.commentCount}}

{{comment.name}}
{{comment.os}}
{{comment.browser}}

{{comment.reply.name}}
{{comment.reply.os}}
{{comment.reply.browser}}
