ZeroC Ice连接事件

时间:2017-06-14 15:48:20

标签: ice

如果客户端与服务器连接或断开连接,我如何知道服务器端?

ZeroC Ice是否为这些事件提供了一些回调函数?

最小的演示:

服务器端

try(com.zeroc.Ice.Communicator communicator = com.zeroc.Ice.Util.initialize(args))
    {
        com.zeroc.Ice.ObjectAdapter adapter =
            communicator.createObjectAdapterWithEndpoints("Hello", "default -h localhost -p 10000");
        adapter.add(new HelloI(), com.zeroc.Ice.Util.stringToIdentity("hello"));
        adapter.activate();
        communicator.waitForShutdown();
    }

客户端

    try(com.zeroc.Ice.Communicator communicator = com.zeroc.Ice.Util.initialize(args))
    {
        HelloPrx hello = HelloPrx.checkedCast(communicator.stringToProxy("hello:default -h localhost -p 10000"));
        hello.sayHello();
    }

1 个答案:

答案 0 :(得分:0)

从服务器端,您可以检查current.con成员并注册一个关闭回调,如果它是之前未见过的连接,请参阅setCloseCallback

当前是com.zeroc.Ice.Current参数传递给所有服务方方法,例如在sayHello实现中你可以做

@Override
public void sayHello(int delay, com.zeroc.Ice.Current current)
{
    // Check if current.con is a new connection, you will 
    // need to keep track of that in your code.

    // Install a close callback for the connection.
    current.con.setCloseCallback(con ->
       {
            // Close callback
       });
}