use of com.jme3.network.HostedConnection in project TeachingInSimulation by ScOrPiOzzy.
the class LoginMessageHandler method execute.
@Override
public void execute(HostedConnection source, LoginMessage m) throws Exception {
// 接收客户端的请求信息
String code = m.getUserCode();
String passwd = m.getUserPwd();
if (code == null || passwd == null) {
return;
}
// 验证用户信息
// 准备一个消息用作服务器的响应消息
LoginMessage respMsg = (LoginMessage) m;
try {
final User user = userService.login(code, passwd);
List<HostedConnection> clients = serverConfig.getClients();
// 进一步验证
if (clients.size() >= serverConfig.getMaxLogin()) {
// 告诉这个用户,当前用户已满,
respMsg.setResult(LoginResult.MAX_SIZE);
source.send(respMsg);
LOG.warn("客户端数量已经达到上限:{}", clients.size());
return;
}
// 检查用户是否已经登录了
HostedConnection existConn = clients.stream().filter(c -> user.getId().equals(c.getAttribute(Session.KEY_LOGIN_ID.name()))).findAny().orElse(null);
if (existConn == null) {
source.setAttribute(Session.KEY_LOGIN_ID.name(), user.getId());
source.setAttribute(Session.KEY_LOGIN_CLASSID.name(), user.getClassId());
source.setAttribute(Session.KEY_LOGIN_ACCOUNT.name(), user.getCode());
clients.add(source);
LOG.info("客户端登录成功,当前客户端数量{}", clients.size());
// 用户成功连接
respMsg.setResult(LoginResult.SUCCESS);
respMsg.setUserId(user.getId());
respMsg.setUserType(user.getRole());
source.send(respMsg);
} else if (m.isFocus()) {
DisconnectMessage disconnect = new DisconnectMessage();
disconnect.setReason(DisconnectMessage.KICK);
disconnect.setType(DisconnectMessage.KICK);
existConn.send(disconnect);
source.setAttribute(Session.KEY_LOGIN_ID.name(), user.getId());
source.setAttribute(Session.KEY_LOGIN_CLASSID.name(), user.getClassId());
source.setAttribute(Session.KEY_LOGIN_ACCOUNT.name(), user.getCode());
clients.add(source);
LOG.info("客户端登录成功,当前客户端数量{}", clients.size());
// 用户成功连接
respMsg.setResult(LoginResult.SUCCESS);
respMsg.setUserId(user.getId());
respMsg.setUserType(user.getRole());
source.send(respMsg);
} else {
// 用户已经登录了
respMsg.setResult(LoginResult.DUPLICATE);
source.send(respMsg);
}
} catch (ServiceException e) {
// 登录失败:原因是登录信息错误
respMsg.setResult(LoginResult.FAILURE);
source.send(respMsg);
throw e;
} catch (Exception e) {
// 登录失败:服务器出现异常
respMsg.setResult(LoginResult.SERVER_EXCE);
source.send(respMsg);
throw e;
}
}
use of com.jme3.network.HostedConnection in project TeachingInSimulation by ScOrPiOzzy.
the class LibraryPublishServiceImpl method publishLibraryToClass.
@Override
public Integer publishLibraryToClass(LibraryPublish publish) {
// 记录考核发布记录
saveUseGeneratedKeys(publish);
// 广播开始考核
ExamMessage message = new ExamMessage();
message.setPid(publish.getId());
message.setType(ExamMessage.EXAM_START);
// 筛选当前登陆人员
List<HostedConnection> collection = new ArrayList<>();
for (HostedConnection hostedConnection : serverConfig.getClients()) {
if (publish.getClassId().equals(hostedConnection.getAttribute(Session.KEY_LOGIN_CLASSID.name()))) {
collection.add(hostedConnection);
}
}
serverConfig.getServer().broadcast(Filters.in(collection), message);
return publish.getId();
}
use of com.jme3.network.HostedConnection in project TeachingInSimulation by ScOrPiOzzy.
the class LinkMsgHandler method execute.
@Override
public void execute(HostedConnection source, Message m) {
LinkerMsg reqMsg = (LinkerMsg) m;
String myLeader = source.getAttribute(GROUP_LEADER_ID);
if (Util.isEmpty(myLeader)) {
throw new RuntimeException("在线连线状态下参数:GROUP_LEADER_ID不可能为空!");
}
String userid = source.getAttribute(PERSONAL_ID);
Collection<HostedConnection> conns = source.getServer().getConnections();
String leaderid = null;
String loginid = null;
for (HostedConnection conn : conns) {
leaderid = conn.getAttribute(GROUP_LEADER_ID);
loginid = conn.getAttribute(PERSONAL_ID);
if (!myLeader.equals(leaderid) || userid.equals(loginid)) {
continue;
}
LinkerMsg respMsg = reqMsg;
conn.send(respMsg);
}
}
Aggregations