我正在尝试在Java服务器和JavaScript客户端之间建立连接,但我在客户端遇到此错误:
与'ws://127.0.0.1:4444 /'的WebSocket连接失败:连接在收到握手响应之前关闭
它可能保持在OPENNING状态,因为connection.onopen从不调用该函数.该console.log('Connected!')不会被调用.
有人能告诉我这里出了什么问题吗?
服务器
import java.io.IOException;
import java.net.ServerSocket;
public class Server {
public static void main(String[] args) throws IOException {
try (ServerSocket serverSocket = new ServerSocket(4444)) {
GameProtocol gp = new GameProtocol();
ServerThread player= new ServerThread(serverSocket.accept(), gp);
player.start();
} catch (IOException e) {
System.out.println("Could not listen on port: 4444");
System.exit(-1);
}
}
}
Run Code Online (Sandbox Code Playgroud)
ServerThread
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ServerThread extends Thread{
private Socket socket = null; …Run Code Online (Sandbox Code Playgroud)