使用tyrus的客户端/服务器java websocket

时间:2016-04-15 20:07:08

标签: java websocket glassfish tyrus

我一直在讨论在这里询问有关在java中使用websockets的所有问题,但似乎没有任何问题可以解释我的问题。我有一个部署到glassfish 4.0的服务器,删除了上下文根,或者基本上设置为“”。这是由项目负责人完成的,其他原因与我的代码无关。我已将服务器发布到本地glassfish安装,并使用简单的“ping”方法验证了这一点。这是我的服务器端点代码。

@ServerEndpoint("/test1")
public class UploadWebSocketEndpoint implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 9054820114345305531L;

public UploadWebSocketEndpoint() { super(); }

@OnOpen
public void open(Session session)
{
    if(session != null)
    {
        System.out.println(session.getId() + " has opened a connection");
        try
        {
//              session.getBasicRemote().sendText("Connection Established");
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

@OnClose
public void close(Session session)
{
    if(session != null)
    {
        System.out.println(session.getId() + " has closed the session");
        try
        {
//              session.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "ended process"));
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

@OnMessage
public String requestAccess(String message, Session session)
{
    System.out.println("recieved message: " + message + " from user " + session.getId());
    return "";
}

@OnError
public void onError(Throwable t)
{
    new Exception(t).getStackTrace();
}

}

在查看尽可能多的示例和我能找到的所有文档之后,这似乎是正确的。现在这里是使用tyrus客户端独立jar的客户端代码。

@ClientEndpoint
public class ClientEndpointSocket
{
Session userSession = null;
private MessageHandler messageHandler;

public ClientEndpointSocket(URI endpointURI)
{
    try
    {
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        System.out.println("conatiner: " + ContainerProvider.getWebSocketContainer().getClass());
        System.out.println("attempting to connect to server at endpoint: " + endpointURI.toString());
        container.connectToServer(this, endpointURI);
    }catch(Exception e)
    {
        e.printStackTrace();
    }
}

@OnOpen
public void onOpen(Session session)
{
    try
    {
        System.out.println("opening web socket");
        this.userSession = session;
    }catch(Exception e)
    {
        e.printStackTrace();
    }
}


@OnMessage
public String onMessage(String message, Session session)
{
    if(this.messageHandler != null)
    {
        this.messageHandler.handleMessage(message);
    }
    return null;
}


@OnClose
public void onClose(Session session, CloseReason closeReason)
{
    System.out.println("closing websocket");
    this.userSession = null;
}

public void sendMessage(String message)
{
    this.userSession.getAsyncRemote().sendText(message);
}

public void addMessageHandler(MessageHandler msgHandler)
{
    this.messageHandler = msgHandler;
}

public static interface MessageHandler 
{
    public void handleMessage(String message);
}
}

这是我的客户主要课程

public class EncryptionMain  {


public static void main(String[] args)
{
    try
    {   

        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        String uri = "ws://localhost:8080/test1";
        System.out.println("connecting to server: " + uri);
        container.connectToServer(ClientEndpointSocket.class, URI.create(uri)); 

    }catch(Exception e)
    {
        e.printStackTrace();
    }

}
}

最后,堆栈跟踪:

javax.websocket.DeploymentException: Handshake error.
at org.glassfish.tyrus.client.ClientManager$3$1.run(ClientManager.java:674)
at org.glassfish.tyrus.client.ClientManager$3.run(ClientManager.java:712)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.glassfish.tyrus.client.ClientManager$SameThreadExecutorService.execute(ClientManager.java:866)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
at org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:511)
at org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:317)
at main.java.application.EncryptionMain.main(EncryptionMain.java:34)
Caused by: org.glassfish.tyrus.core.HandshakeException: Response code was not 101: 404.
at org.glassfish.tyrus.client.TyrusClientEngine.processResponse(TyrusClientEngine.java:320)
at org.glassfish.tyrus.container.grizzly.client.GrizzlyClientFilter.handleHandshake(GrizzlyClientFilter.java:346)
at org.glassfish.tyrus.container.grizzly.client.GrizzlyClientFilter.handleRead(GrizzlyClientFilter.java:315)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)

如果您需要有关该项目的更多信息,请告诉我。有了404错误,我认为我的网址是不正确的,虽然我已经测试了服务器,它确实驻留在localhost / app的http资源,根据我的理解,这意味着ws:将位于localhost:8080 /“here”。如果此处的任何信息不正确或缺乏,请再次告知我们。感谢您提前提供任何帮助

0 个答案:

没有答案