Skip to content

Commit

Permalink
添加用户状态查询接口
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleCatMySelf committed Oct 15, 2019
1 parent 9c79ecb commit a875fc4
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public void getSize(Channel channel) {
httpChannel.getSize(channel);
}

@Override
public void getState(Channel channel, SendServerVO sendServerVO) {
httpChannel.getState(channel, sendServerVO);
}

@Override
public void sendFromServer(Channel channel, SendServerVO serverVO) {
httpChannel.sendFromServer(channel,serverVO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ public void getList(Channel channel) {
close(channel);
}

@Override
public void getState(Channel channel, SendServerVO serverVO) {
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
response.headers().set(HttpConstant.CONTENT_TYPE,HttpConstant.APPLICATION_JSON);
if (serverVO.getToken() == ""){
notFindUri(channel);
}
Boolean state = WebSocketCacheMap.hasToken(serverVO.getToken());
StateVo stateVo = new StateVo(serverVO.getToken(),state);
ResultVO<StateVo> resultVO = new ResultVO<>(HttpResponseStatus.OK.code(),stateVo);
Gson gson = new Gson();
ByteBuf buf = Unpooled.copiedBuffer(gson.toJson(resultVO), CharsetUtil.UTF_8);
response.content().writeBytes(buf);
channel.writeAndFlush(response);
close(channel);
}

@Override
public void sendInChat(String token, Map msg) {
String address = RedisUtil.getAddress(RedisUtil.convertMD5(WebSocketCacheMap.getByJedis(token)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public interface HttpChannel {

void getList(Channel channel);

void getState(Channel channel,SendServerVO serverVO);

void sendInChat(String token, Map msg);

void sendByInChat(Channel channel,SendInChat sendInChat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ protected void httpdoMessage(ChannelHandlerContext ctx, FullHttpRequest msg) {
throw new HandlerNotFoundException(UndefinedInChatConstant.NOT_HANDLER);
}
switch (HttpUtil.checkType(msg)){
/** 获取在线用户数 */
case HttpConstant.GET_SIZE:
log.info(LogConstant.DEFAULTWEBSOCKETHANDLER_GETSIZE);
httpHandlerService.getSize(channel);
break;
/** 以服务端形式发送出去 */
case HttpConstant.SEND_FROM_SERVER:
log.info(LogConstant.DEFAULTWEBSOCKETHANDLER_SENDFROMSERVER);
SendServerVO serverVO = null;
Expand All @@ -75,14 +77,28 @@ protected void httpdoMessage(ChannelHandlerContext ctx, FullHttpRequest msg) {
}
httpHandlerService.sendFromServer(channel,serverVO);
break;
/** 获取在线用户列表 */
case HttpConstant.GET_LIST:
log.info(LogConstant.DEFAULTWEBSOCKETHANDLER_GETLIST);
httpHandlerService.getList(channel);
break;
/** 获取用户在线状态 */
case HttpConstant.GET_STATE:
log.info(LogConstant.DEFAULTWEBSOCKETHANDLER_GETSTATE);
SendServerVO token = null;
try {
token = HttpUtil.getToken(msg);
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage());
}
httpHandlerService.getState(channel,token);
break;
/** 分布式通讯转接 */
case HttpConstant.SEND_IN_CHAT:
log.info(LogConstant.DEFAULTWEBSOCKETHANDLER_SENDINCHAT);
httpHandlerService.sendInChat(channel,msg);
break;
/** 未匹配到uri */
case HttpConstant.NOT_FIND_URI:
log.info(LogConstant.DEFAULTWEBSOCKETHANDLER_NOTFINDURI);
httpHandlerService.notFindUri(channel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
*/
public class GetListVO {



private Set<String> tokens;

public GetListVO(Set<String> tokens) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.unclecatmyself.common.bean.vo;

/**
* @ClassName StateVo
* @Description TODO
* @Author MySelf
* @Date 2019/10/15 23:27
* @Version 1.0
**/
public class StateVo {

public String token;

public Boolean state;

public StateVo() {
}

public StateVo(String token, Boolean state) {
this.token = token;
this.state = state;
}

public String getToken() {
return token;
}

public void setToken(String token) {
this.token = token;
}

public Boolean getState() {
return state;
}

public void setState(Boolean state) {
this.state = state;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class HttpConstant {

public static final String GET_LIST = "get_list";

public static final String GET_STATE = "get_state";

public static final String SEND_IN_CHAT = "send_inChat";

public static final String SEND_FROM_SERVER = "send_from_server";
Expand All @@ -21,6 +23,8 @@ public class HttpConstant {

public static final String URI_GET_SIZE = "/uri_get_size";

public static final String URI_GET_STATE = "/uri_get_state";

public static final String URI_GET_LIST = "/uri_get_list";

public static final String URI_SEND_IN_CHAT = "/uri_send_inChat";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class LogConstant {

public static final String DEFAULTWEBSOCKETHANDLER_GETLIST = "[DefaultWebSocketHandler.httpdoMessage.GET_LIST]";

public static final String DEFAULTWEBSOCKETHANDLER_GETSTATE = "[DefaultWebSocketHandler.httpdoMessage.GET_STATE]";

public static final String DEFAULTWEBSOCKETHANDLER_SENDINCHAT = "[DefaultWebSocketHandler.httpdoMessage.SEND_IN_CHAT]";

public static final String DEFAULTWEBSOCKETHANDLER_LOGIN = "[DefaultWebSocketHandler.textdoMessage.LOGIN]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public static String checkType(FullHttpRequest msg){
return HttpConstant.SEND_FROM_SERVER;
}else if (url.equals(HttpConstant.URI_GET_LIST) && meName.equals(HttpConstant.GET)){
return HttpConstant.GET_LIST;
}else if (url.equals(HttpConstant.URI_GET_STATE) && meName.equals(HttpConstant.POST)){
return HttpConstant.GET_STATE;
}else if (url.equals(HttpConstant.URI_SEND_IN_CHAT) && meName.equals(HttpConstant.POST)){
return HttpConstant.SEND_IN_CHAT;
}else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public abstract class HandlerService implements Handler {
*/
public abstract void getSize(Channel channel);

/**
* HTTP获取用户状态
* @param channel {@link Channel} 链接实例
* @param sendServerVO {@link SendServerVO} 用户标识
*/
public abstract void getState(Channel channel,SendServerVO sendServerVO);

/**
* HTTP以服务端向指定用户发送通知
* @param channel {@link Channel} 链接实例
Expand Down

0 comments on commit a875fc4

Please sign in to comment.