ServerApp:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.glassfish.tyrus.server.Server;
public class ServerApp {
public static void main(final String[] args) {
ServerApp server = new ServerApp();
server.runServer();
}
public void runServer() {
Server server = new Server("localhost", 8025, "/websocket", null, ServerEndPoint.class);
try {
server.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please press a key to stop the server.");
reader.readLine();
} catch (Exception e) {
e.printStackTrace();
} finally {
server.stop();
}
}
}
ServerEndPoint:
import java.io.IOException;
import java.util.logging.Logger;
import javax.websocket.CloseReason;
import javax.websocket.CloseReason.CloseCodes;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint(value = "/game")
public class ServerEndPoint {
private final Logger logger = Logger.getLogger(this.getClass().getName());
@OnOpen
public void onOpen(Session session) {
System.out.println("Connected ... " + session.getId());
}
@OnMessage
public String onMessage(String message, Session session) {
System.out.println("onMessage: "+message);
switch (message) {
case "quit":
try {
session.close(new CloseReason(CloseCodes.NORMAL_CLOSURE, "Game ended"));
} catch (IOException e) {
throw new RuntimeException(e);
}
break;
}
return message;
}
@OnClose
public void onClose(Session session, CloseReason closeReason) {
System.out.println("onClose: "+String.format("Session %s closed because of %s", session.getId(), closeReason));
logger.info(String.format("Session %s closed because of %s", session.getId(), closeReason));
}
}
这很有效,服务器启动并等待客户端。
ClientApp:
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.CountDownLatch;
import javax.websocket.ClientEndpoint;
import javax.websocket.DeploymentException;
import org.glassfish.tyrus.client.ClientManager;
@ClientEndpoint
public class ClientApp {
public static CountDownLatch latch;
public static void main(String[] args) throws IOException {
latch = new CountDownLatch(1);
ClientManager client = ClientManager.createClient();
try {
client.connectToServer(ClientApp.class, new URI("ws://localhost:8025/websockets/game"));
latch.await();
} catch (DeploymentException | URISyntaxException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}
ClientEndPoint:
import java.util.logging.Logger;
import javax.websocket.ClientEndpoint;
import javax.websocket.CloseReason;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
@ClientEndpoint
public class ClientEndPoint {
private final Logger logger = Logger.getLogger(this.getClass().getName());
@OnOpen
public void onOpen(Session session) {
// same as above
System.out.println("onOpen");
}
@OnMessage
public String onMessage(String message, Session session) {
// same as above
System.out.println("onMessage");
return "";
}
@OnClose
public void onClose(Session session, CloseReason closeReason) {
System.out.println("onClose");
logger.info(String.format("Session %s close because of %s", session.getId(), closeReason));
ClientApp.latch.countDown();
}
}
当我尝试连接时:Exception in thread "main" java.lang.RuntimeException: javax.websocket.DeploymentException: Handshake error.
我不知道是什么造成的。
跟踪日志: 线程“main”中的异常java.lang.RuntimeException:javax.websocket.DeploymentException:握手错误。 在* .websocket.client.ClientApp.main(ClientApp.java:23) 引起:javax.websocket.DeploymentException:握手错误。 在org.glassfish.tyrus.client.ClientManager $ 1 $ 1.run(ClientManager.java:552) 在org.glassfish.tyrus.client.ClientManager $ 1.run(ClientManager.java:582) at java.util.concurrent.Executors $ RunnableAdapter.call(Unknown Source) at java.util.concurrent.FutureTask.run(Unknown Source) 在org.glassfish.tyrus.client.ClientManager $ SameThreadExecutorService.execute(ClientManager.java:734) at java.util.concurrent.AbstractExecutorService.submit(Unknown Source) 在org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:431) 在org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:267) 在* .websocket.client.ClientApp.main(ClientApp.java:19) 引起:org.glassfish.tyrus.core.HandshakeException:响应代码不是101:404。 在org.glassfish.tyrus.core.Handshake.validateServerResponse(Handshake.java:194) 在org.glassfish.tyrus.client.TyrusClientEngine.processResponse(TyrusClientEngine.java:142) 在org.glassfish.tyrus.container.grizzly.client.GrizzlyClientFilter.handleHandshake(GrizzlyClientFilter.java:326) 在org.glassfish.tyrus.container.grizzly.client.GrizzlyClientFilter.handleRead(GrizzlyClientFilter.java:296) 在org.glassfish.grizzly.filterchain.ExecutorResolver $ 9.execute(ExecutorResolver.java:119) 在org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284) 在org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201) 在org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133) 在org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112) 在org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) 在org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561) 在org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112) 在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117) 在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access $ 100(WorkerThreadIOStrategy.java:56) 在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy $ WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137) 在org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:565) 在org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:545) 在java.lang.Thread.run(未知来源)