相关疑难解决方法(0)

Java服务器JavaScript客户端WebSockets

我正在尝试在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)

javascript java websocket java-websocket

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×1

java-websocket ×1

javascript ×1

websocket ×1