我正在使用 websocket4j 库,并查看了有关如何在服务器应用程序中使用它的示例。我从那里获取代码并将其实施到我当前使用原始套接字的当前项目中。无论我使用哪个客户端,我似乎都会收到一个 IOExcpetion 说握手失败。我不知道出了什么问题,因为我从示例服务器中获取了代码(这显然应该可以工作),并且我使用了一个 javascript 示例客户端来确保它不是客户端的错。这是主要方法。
public static void main(String args[]){
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
System.out.print("PORT ( default - 1000 ): ");
int port=1000;
try{
port=Integer.parseInt(b.readLine());
}
catch(Exception e){
System.out.println("Wrong format. Using 1000 instead");
port=1000;
}
System.out.println("Server Started. Waiting for connections");
try{
WebServerSocket listener=new WebServerSocket(port);
while(true){
WebSocket server=listener.accept();
U413ChatServer connection=new U413ChatServer(server);
Thread t=new Thread(connection);
t.start();
}
}
catch(Exception e){ e.printStackTrace();
}
}
这是屏幕上出现的内容
Server Started. Waiting for connections
java.io.IOException: Handshake failed
at websocket4j.AbstractWebSocket.<init>(Unknown Source)
at websocket4j.server.WebSocket.<init>(Unknown Source)
at websocket4j.server.WebServerSocket.accept(Unknown Source)
at U413ChatServer.main(U413ChatServer.java:55)
Caused by: java.io.IOException: Unexpected header field: Sec-WebSocket-Key: yfOg
xCbblVyY8ARVFyMtOw==
at websocket4j.server.WebSocket.handshake(Unknown Source)
at websocket4j.AbstractWebSocket$HandshakeRunner.run(Unknown Source)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:47
1)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.lang.Thread.run(Thread.java:722)
第 55 行是:
WebSocket server=listener.accept();
任何帮助将非常感激