use of com.cas.sim.tis.message.handler.ServerHandler in project TeachingInSimulation by ScOrPiOzzy.
the class ServerConfig method createDataServer.
@Bean
public Server createDataServer() throws IOException {
server = Network.createServer(SystemInfo.APP_NAME, SystemInfo.APP_VERSION, port, -1);
LOG.info("主服务器地址:{}", InetAddress.getLocalHost());
LOG.info("主服务器监听在{}端口上", port);
server.addConnectionListener(new ConnectionListener() {
@Override
public void connectionAdded(Server server, HostedConnection conn) {
LOG.info("用户{}已连接", conn.getAddress());
}
@Override
public void connectionRemoved(Server server, HostedConnection conn) {
boolean success = clients.remove(conn);
if (success) {
String account = conn.getAttribute(Session.KEY_LOGIN_ACCOUNT.name());
LOG.info("用户{}已断开连接, 当前客户端数量{}", account, clients.size());
}
}
});
server.addMessageListener(new MessageListener<HostedConnection>() {
@Override
public void messageReceived(HostedConnection client, Message m) {
LOG.info("收到客户端{}的消息{}", client.getAddress(), m.getClass().getName());
ServerHandler<Message> handler = messageHandlerClass.get(m.getClass());
if (handler != null) {
try {
handler.execute(client, m);
LOG.debug("消息处理成功");
} catch (Exception e) {
LOG.warn("消息处理失败", e);
}
} else {
LOG.error("无法处理消息{},缺少相应的处理类,Eg:public class {} implements ServerHandler\\{\\}", m.getClass(), m.getClass().getSimpleName());
}
}
});
return server;
}
Aggregations