JSR-356网络套接字-停止容器后无法获取返回代码

问题描述

我已经使用 javax websockets 构建了一个客户端服务器应用程序。尽管JSR 356没有停止websocket容器的方法,但我设法在将其投射到 jetty Lifecyle 之后停止了该容器em>。一旦停止我写的容器

system.exit(1);

但是从客户端程序返回的代码始终为零。

代码: @ClientEndpoint

    public boolean close()
        {
            try
              {
                  if(this.container != null && this.container instanceof LifeCycle) { 
                      logger.trace("stoping container...");
                      ((LifeCycle) this.container).stop();
                  }

                  this.session.close();
        
                return true;
            }
            catch (Exception e)
            {
                logger.trace("Exception occured while closing the connection",e);
                return false;
            }
        }

code:从另一个类调用close方法

public closeConnection(){

     this.wc.close();
     System.exit(1);

}

感谢您的帮助:-)

解决方法

您是要尝试从属于容器的线程还是容器的ThreadPool / Executor调用closeConnectionclose

此外,一旦容器停止,所有会话也将停止。

@ClientEndpoint close()更改为...

public void close()
{
    try
    {
        this.session.close();
    }
    catch (Exception e)
    {
        logger.trace("Exception occured while closing the connection",e);
    }
}

然后简单地使closeConnection() ...

public closeConnection()
{
    LifeCycle.stop(this.wc.getContainer());
    System.exit(1);
}

可以在
找到一个可行的例子 https://github.com/joakime/javaxwebsocket-client-returncode

其中有代码...

App.java

package org.eclipse.jetty.demo;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.websocket.ClientEndpointConfig;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.WebSocketContainer;

import org.eclipse.jetty.util.component.LifeCycle;

public class App
{
    private static final App MAIN_INSTANCE = new App();

    private WebSocketContainer client;

    public static void main(String[] args) throws IOException,DeploymentException,URISyntaxException
    {
        App.MAIN_INSTANCE.connect();
    }

    public static void stop(int returnCode)
    {
        // Trigger stop from thread that does not belong to Container.
        new Thread(() ->
        {
            LifeCycle.stop(App.MAIN_INSTANCE.client);
            System.exit(returnCode);
        }).start();
    }

    private WebSocketContainer getClientContainer()
    {
        if (client == null)
            client = ContainerProvider.getWebSocketContainer();
        return client;
    }

    public void connect() throws IOException,URISyntaxException
    {
        URI echoUri = URI.create("wss://echo.websocket.org");

        ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create()
            .configurator(new OriginServerConfigurator(echoUri))
            .build();

        EchoEndpoint echoEndpoint = new EchoEndpoint();
        getClientContainer().connectToServer(echoEndpoint,clientEndpointConfig,echoUri);
        System.out.printf("Connected to : %s%n",echoUri);
    }
}

EchoEndpoint.java

package org.eclipse.jetty.demo;

import javax.websocket.CloseReason;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
import javax.websocket.Session;

/**
 * Basic Echo Client Endpoint
 */
public class EchoEndpoint extends Endpoint implements MessageHandler.Whole<String>
{
    private Session session;

    @Override
    public void onClose(Session session,CloseReason closeReason)
    {
        System.out.printf("Connection closed: Session.id=%s - %s%n",session.getId(),closeReason);
        this.session = null;
    }

    @Override
    public void onOpen(Session session,EndpointConfig config)
    {
        System.out.printf("Got open: Session.id=%s%n",session.getId());
        this.session = session;
        this.session.addMessageHandler(this);
        try
        {
            session.getBasicRemote().sendText("Hello");
            session.getBasicRemote().sendText("How are things?");
            session.getBasicRemote().sendText("Thanks for the conversation.");
        }
        catch (Throwable t)
        {
            t.printStackTrace();
        }
    }

    @Override
    public void onMessage(String msg)
    {
        System.out.printf("Got msg: \"%s\"%n",msg);
        if (msg.contains("Thanks"))
        {
            App.stop(1);
        }
    }

    @Override
    public void onError(Session session,Throwable cause)
    {
        cause.printStackTrace();
    }
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...