Search in sources :

Example 1 with ConnectionListener

use of com.jme3.network.ConnectionListener 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;
}
Also used : Server(com.jme3.network.Server) FtpServer(org.apache.ftpserver.FtpServer) Message(com.jme3.network.Message) HostedConnection(com.jme3.network.HostedConnection) ConnectionListener(com.jme3.network.ConnectionListener) ServerHandler(com.cas.sim.tis.message.handler.ServerHandler) IOException(java.io.IOException) Bean(org.springframework.context.annotation.Bean)

Aggregations

ServerHandler (com.cas.sim.tis.message.handler.ServerHandler)1 ConnectionListener (com.jme3.network.ConnectionListener)1 HostedConnection (com.jme3.network.HostedConnection)1 Message (com.jme3.network.Message)1 Server (com.jme3.network.Server)1 IOException (java.io.IOException)1 FtpServer (org.apache.ftpserver.FtpServer)1 Bean (org.springframework.context.annotation.Bean)1