Skip to content

Listening The Connection

徐昊 edited this page Nov 11, 2018 · 3 revisions
  • If you need to use the SocketServer feature, you need to integrate Server-Plugin. According to the integration documentation. Integration WIKI

First Get Register

//You should prepare a unused port, and get `IRegister` first
IRegister register = OkSocket.server(/*port*/);

Through Register To Get Manager

IServerManager serverManager = register.registerReceiver(new ServerActionAdapter() {
            @Override
            public void onServerListening(int serverPort) {
                //This method will be called back when the server listens for the port you specified successfully.
            }

            @Override
            public void onServerWillBeShutdown(int serverPort, IServerShutdown shutdown, IClientPool clientPool, Throwable throwable) {
                //This method will be called back when the server has to be closed due to exception information. 
                //You should complete the server save and close work in this method.
                //You should call the server's `shutdown()` method after finishing the finishing work.
                shutdown.shutdown();
            }

            @Override
            public void onServerAlreadyShutdown(int serverPort) {
                //This method will be called back when you call the shutdown method.
            }
        });

Then Start Listen

 if (!serverManager.isLive()) {
     //Start listen the specific port
     serverManager.listen();
 }