添加设置/获取备注api,添加用户在线状态回调功能
This commit is contained in:
@@ -133,7 +133,7 @@ push.ios.server.address http://localhost:8085/ios/push
|
||||
# User online status event callback
|
||||
#*********************************************************************
|
||||
##用户在线状态事件回调地址
|
||||
user.online_status_callback http://localhost:8085/user/online_event
|
||||
user.online_status_callback http://localhost:8888/user/online_event
|
||||
|
||||
#*********************************************************************
|
||||
# MongoDB configuration
|
||||
|
||||
@@ -9,7 +9,9 @@
|
||||
package com.xiaoleilu.loServer.action.admin;
|
||||
|
||||
import cn.wildfirechat.common.APIPath;
|
||||
import cn.wildfirechat.pojos.InputGetAlias;
|
||||
import cn.wildfirechat.pojos.InputGetFriendList;
|
||||
import cn.wildfirechat.pojos.OutputGetAlias;
|
||||
import cn.wildfirechat.pojos.OutputStringList;
|
||||
import com.google.gson.Gson;
|
||||
import com.xiaoleilu.loServer.RestResult;
|
||||
@@ -36,16 +38,19 @@ public class AliasGetAction extends AdminAction {
|
||||
@Override
|
||||
public boolean action(Request request, Response response) {
|
||||
if (request.getNettyRequest() instanceof FullHttpRequest) {
|
||||
InputGetFriendList inputGetFriendList = getRequestBody(request.getNettyRequest(), InputGetFriendList.class);
|
||||
List<FriendData> dataList = messagesStore.getFriendList(inputGetFriendList.getUserId(), null, 0);
|
||||
InputGetAlias input = getRequestBody(request.getNettyRequest(), InputGetAlias.class);
|
||||
List<FriendData> dataList = messagesStore.getFriendList(input.getOperator(), null, 0);
|
||||
List<String> list = new ArrayList<>();
|
||||
OutputGetAlias out = new OutputGetAlias(input.getOperator(), input.getTargetId());
|
||||
|
||||
for (FriendData data : dataList) {
|
||||
if (data.getState() == inputGetFriendList.getStatus()) {
|
||||
list.add(data.getFriendUid());
|
||||
if (data.getFriendUid().equals(input.getTargetId())) {
|
||||
out.setAlias(data.getAlias());
|
||||
break;
|
||||
}
|
||||
}
|
||||
response.setStatus(HttpResponseStatus.OK);
|
||||
RestResult result = RestResult.ok(new OutputStringList(list));
|
||||
RestResult result = RestResult.ok(out);
|
||||
response.setContent(new Gson().toJson(result));
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -9,7 +9,9 @@
|
||||
package com.xiaoleilu.loServer.action.admin;
|
||||
|
||||
import cn.wildfirechat.common.APIPath;
|
||||
import cn.wildfirechat.common.ErrorCode;
|
||||
import cn.wildfirechat.pojos.InputGetFriendList;
|
||||
import cn.wildfirechat.pojos.InputUpdateAlias;
|
||||
import cn.wildfirechat.pojos.OutputStringList;
|
||||
import com.google.gson.Gson;
|
||||
import com.xiaoleilu.loServer.RestResult;
|
||||
@@ -36,16 +38,10 @@ public class AliasPutAction extends AdminAction {
|
||||
@Override
|
||||
public boolean action(Request request, Response response) {
|
||||
if (request.getNettyRequest() instanceof FullHttpRequest) {
|
||||
InputGetFriendList inputGetFriendList = getRequestBody(request.getNettyRequest(), InputGetFriendList.class);
|
||||
List<FriendData> dataList = messagesStore.getFriendList(inputGetFriendList.getUserId(), null, 0);
|
||||
List<String> list = new ArrayList<>();
|
||||
for (FriendData data : dataList) {
|
||||
if (data.getState() == inputGetFriendList.getStatus()) {
|
||||
list.add(data.getFriendUid());
|
||||
}
|
||||
}
|
||||
InputUpdateAlias input = getRequestBody(request.getNettyRequest(), InputUpdateAlias.class);
|
||||
ErrorCode errorCode = messagesStore.setFriendAliasRequest(input.getOperator(), input.getTargetId(), input.getAlias(), new long[1]);
|
||||
response.setStatus(HttpResponseStatus.OK);
|
||||
RestResult result = RestResult.ok(new OutputStringList(list));
|
||||
RestResult result = RestResult.resultOf(errorCode);
|
||||
response.setContent(new Gson().toJson(result));
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -103,7 +103,7 @@ public class NettyMQTTHandler extends ChannelInboundHandlerAdapter {
|
||||
String clientID = NettyUtils.clientID(ctx.channel());
|
||||
if (clientID != null && !clientID.isEmpty()) {
|
||||
LOG.info("Notifying connection lost event. MqttClientId = {}.", clientID);
|
||||
m_processor.processConnectionLost(clientID, ctx.channel());
|
||||
m_processor.processConnectionLost(clientID, ctx.channel(), false);
|
||||
}
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@@ -248,24 +248,36 @@ public class ProtocolProcessor {
|
||||
MemorySessionStore.Session session = m_sessionsStore.getSession(clientId);
|
||||
if(session != null) {
|
||||
session.refreshLastActiveTime();
|
||||
forwardOnlineStatusEvent(payload.userName(), clientId, session.getPlatform(), UserOnlineStatus.ONLINE);
|
||||
} else {
|
||||
forwardOnlineStatusEvent(payload.userName(), clientId, ProtoConstants.Platform.Platform_UNSET, UserOnlineStatus.ONLINE);
|
||||
}
|
||||
|
||||
LOG.info("The CONNECT message has been processed. CId={}, username={}", clientId, payload.userName());
|
||||
}
|
||||
|
||||
private static class UserOnlineStatus {
|
||||
public static final int ONLINE = 0;
|
||||
public static final int OFFLINE = 1;
|
||||
public static final int LOGOUT = -1;
|
||||
public String userId;
|
||||
public int status; //0 offline; 1 online; -1 logout
|
||||
public String clientId;
|
||||
public int platform;
|
||||
public int status;
|
||||
public long timestamp;
|
||||
|
||||
public UserOnlineStatus(String userId, int status) {
|
||||
public UserOnlineStatus(String userId, String clientId, int platform, int status) {
|
||||
this.userId = userId;
|
||||
this.clientId = clientId;
|
||||
this.platform = platform;
|
||||
this.status = status;
|
||||
this.timestamp = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
public void forwardOnlineStatusEvent(String userId, int status) {
|
||||
public void forwardOnlineStatusEvent(String userId, String clientId, int platform, int status) {
|
||||
if (!StringUtil.isNullOrEmpty(mUserOnlineStatusCallback)) {
|
||||
executorCallback.execute(() -> HttpUtils.httpJsonPost(mUserOnlineStatusCallback, new Gson().toJson(new UserOnlineStatus(userId, status))));
|
||||
executorCallback.execute(() -> HttpUtils.httpJsonPost(mUserOnlineStatusCallback, new Gson().toJson(new UserOnlineStatus(userId, clientId, platform, status))));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -526,7 +538,7 @@ public class ProtocolProcessor {
|
||||
}
|
||||
|
||||
if (!clearSession) {
|
||||
processConnectionLost(clientID, channel);
|
||||
processConnectionLost(clientID, channel, clearSession);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -567,9 +579,16 @@ public class ProtocolProcessor {
|
||||
|
||||
LOG.info("The DISCONNECT message has been processed. CId={}", clientID);
|
||||
|
||||
String username = NettyUtils.userName(channel);
|
||||
MemorySessionStore.Session session = m_sessionsStore.getSession(clientID);
|
||||
if(session != null) {
|
||||
forwardOnlineStatusEvent(username, clientID, session.getPlatform(), UserOnlineStatus.LOGOUT);
|
||||
}
|
||||
|
||||
channel.closeFuture();
|
||||
|
||||
//disconnect the session
|
||||
|
||||
m_sessionsStore.sessionForClient(clientID).disconnect(clearSession);
|
||||
}
|
||||
|
||||
@@ -600,17 +619,21 @@ public class ProtocolProcessor {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void processConnectionLost(String clientID, Channel channel) {
|
||||
public void processConnectionLost(String clientID, Channel channel, boolean clearSession) {
|
||||
LOG.info("Processing connection lost event. CId={}", clientID);
|
||||
|
||||
String username = NettyUtils.userName(channel);
|
||||
MemorySessionStore.Session session = m_sessionsStore.getSession(clientID);
|
||||
if(session != null) {
|
||||
session.refreshLastActiveTime();
|
||||
forwardOnlineStatusEvent(username, clientID, session.getPlatform(), clearSession ? UserOnlineStatus.LOGOUT : UserOnlineStatus.OFFLINE);
|
||||
}
|
||||
|
||||
ConnectionDescriptor oldConnDescr = new ConnectionDescriptor(clientID, channel);
|
||||
if(connectionDescriptors.removeConnection(oldConnDescr)) {
|
||||
MemorySessionStore.Session session = m_sessionsStore.getSession(clientID);
|
||||
if(session != null) {
|
||||
session.refreshLastActiveTime();
|
||||
}
|
||||
String username = NettyUtils.userName(channel);
|
||||
m_interceptor.notifyClientConnectionLost(clientID, username);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* This file is part of the Wildfire Chat package.
|
||||
* (c) Heavyrain2012 <[email protected]>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
package cn.wildfirechat.pojos;
|
||||
|
||||
|
||||
public class InputGetAlias {
|
||||
private String operator;
|
||||
private String targetId;
|
||||
|
||||
public String getTargetId() {
|
||||
return targetId;
|
||||
}
|
||||
|
||||
public void setTargetId(String targetId) {
|
||||
this.targetId = targetId;
|
||||
}
|
||||
|
||||
public String getOperator() {
|
||||
return operator;
|
||||
}
|
||||
|
||||
public void setOperator(String operator) {
|
||||
this.operator = operator;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* This file is part of the Wildfire Chat package.
|
||||
* (c) Heavyrain2012 <[email protected]>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
package cn.wildfirechat.pojos;
|
||||
|
||||
|
||||
public class InputUpdateAlias {
|
||||
private String operator;
|
||||
private String targetId;
|
||||
private String alias;
|
||||
|
||||
public String getTargetId() {
|
||||
return targetId;
|
||||
}
|
||||
|
||||
public void setTargetId(String targetId) {
|
||||
this.targetId = targetId;
|
||||
}
|
||||
|
||||
public String getOperator() {
|
||||
return operator;
|
||||
}
|
||||
|
||||
public void setOperator(String operator) {
|
||||
this.operator = operator;
|
||||
}
|
||||
|
||||
public String getAlias() {
|
||||
return alias;
|
||||
}
|
||||
|
||||
public void setAlias(String alias) {
|
||||
this.alias = alias;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* This file is part of the Wildfire Chat package.
|
||||
* (c) Heavyrain2012 <[email protected]>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
package cn.wildfirechat.pojos;
|
||||
|
||||
|
||||
public class OutputGetAlias {
|
||||
private String operator;
|
||||
private String targetId;
|
||||
private String alias;
|
||||
|
||||
public OutputGetAlias() {
|
||||
}
|
||||
|
||||
public OutputGetAlias(String operator, String targetId) {
|
||||
this.operator = operator;
|
||||
this.targetId = targetId;
|
||||
}
|
||||
|
||||
public String getTargetId() {
|
||||
return targetId;
|
||||
}
|
||||
|
||||
public void setTargetId(String targetId) {
|
||||
this.targetId = targetId;
|
||||
}
|
||||
|
||||
public String getOperator() {
|
||||
return operator;
|
||||
}
|
||||
|
||||
public void setOperator(String operator) {
|
||||
this.operator = operator;
|
||||
}
|
||||
|
||||
public String getAlias() {
|
||||
return alias;
|
||||
}
|
||||
|
||||
public void setAlias(String alias) {
|
||||
this.alias = alias;
|
||||
}
|
||||
}
|
||||
@@ -122,6 +122,14 @@ push.android.server.address http://localhost:8085/android/push
|
||||
##苹果推送服务器地址
|
||||
push.ios.server.address http://localhost:8085/ios/push
|
||||
|
||||
|
||||
#*********************************************************************
|
||||
# User online status event callback
|
||||
#*********************************************************************
|
||||
##用户在线状态事件回调地址
|
||||
user.online_status_callback http://localhost:8888/user/online_event
|
||||
|
||||
|
||||
#*********************************************************************
|
||||
# MongoDB configuration
|
||||
#*********************************************************************
|
||||
|
||||
@@ -6,21 +6,37 @@ import cn.wildfirechat.sdk.model.IMResult;
|
||||
import cn.wildfirechat.sdk.utilities.AdminHttpUtils;
|
||||
|
||||
public class FriendAdmin {
|
||||
public static IMResult<Void> updateFriendStatus(String userId, String targetId, int status) throws Exception {
|
||||
public static IMResult<Void> updateFriendStatus(String operator, String targetId, int status) throws Exception {
|
||||
String path = APIPath.Friend_Update_Status;
|
||||
InputFriendRequest input = new InputFriendRequest();
|
||||
input.setUserId(userId);
|
||||
input.setUserId(operator);
|
||||
input.setFriendUid(targetId);
|
||||
input.setStatus(status);
|
||||
return AdminHttpUtils.httpJsonPost(path, input, Void.class);
|
||||
}
|
||||
|
||||
public static IMResult<OutputStringList> getFriendStatusList(String userId, int status) throws Exception {
|
||||
public static IMResult<OutputStringList> getFriendStatusList(String operator, int status) throws Exception {
|
||||
String path = APIPath.Friend_Get_List;
|
||||
InputGetFriendList input = new InputGetFriendList();
|
||||
input.setUserId(userId);
|
||||
input.setUserId(operator);
|
||||
input.setStatus(status);
|
||||
return AdminHttpUtils.httpJsonPost(path, input, OutputStringList.class);
|
||||
}
|
||||
|
||||
public static IMResult<Void> updateFriendAlias(String operator, String targetId, String alias) throws Exception {
|
||||
String path = APIPath.Friend_Set_Alias;
|
||||
InputUpdateAlias input = new InputUpdateAlias();
|
||||
input.setOperator(operator);
|
||||
input.setTargetId(targetId);
|
||||
input.setAlias(alias);
|
||||
return AdminHttpUtils.httpJsonPost(path, input, Void.class);
|
||||
}
|
||||
|
||||
public static IMResult<OutputGetAlias> getFriendAlias(String operator, String targetId) throws Exception {
|
||||
String path = APIPath.Friend_Get_Alias;
|
||||
InputGetAlias input = new InputGetAlias();
|
||||
input.setOperator(operator);
|
||||
input.setTargetId(targetId);
|
||||
return AdminHttpUtils.httpJsonPost(path, input, OutputGetAlias.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -437,6 +437,23 @@ public class Main {
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
String alias = "hello" + System.currentTimeMillis();
|
||||
IMResult<Void> updateFriendAlias = FriendAdmin.updateFriendAlias("ff1", "ff2", alias);
|
||||
if (updateFriendAlias != null && updateFriendAlias.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
|
||||
System.out.println("update friend status success");
|
||||
} else {
|
||||
System.out.println("update friend status failure");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
IMResult<OutputGetAlias> getFriendAlias = FriendAdmin.getFriendAlias("ff1", "ff2");
|
||||
if (getFriendAlias != null && getFriendAlias.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && getFriendAlias.getResult().getAlias().equals(alias)) {
|
||||
System.out.println("update friend status success");
|
||||
} else {
|
||||
System.out.println("update friend status failure");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
|
||||
//初始化机器人API
|
||||
RobotHttpUtils.init("http://localhost", "robot1", "123456");
|
||||
|
||||
Reference in New Issue
Block a user