我一直在倾诉这里提出的关于在 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 错误,我认为我的 url 不正确,尽管我已经测试了服务器并且它确实驻留在 localhost/app 以获取 http 资源,据我了解,这意味着 ws: 将位于 localhost:8080/“这里”。如果此处的任何信息不正确或缺少,请再次告诉我。感谢您提前提供任何帮助