TooTallNate / Java-WebSocket

A barebones WebSocket client and server implementation written in 100% Java.
http://tootallnate.github.io/Java-WebSocket
MIT License
10.47k stars 2.57k forks source link

The server has connected, but the client hasn't. #1350

Closed yangman92 closed 3 months ago

yangman92 commented 1 year ago

I used a tool as a server. and i used my phone to connect with. The problem now is a the server was connected when i used my phone to connect.

Client coding blow:

public class ClientSocket extends WebSocketClient {

    private final static String TAG = "Socket - ClientSocket";

    private OnClientSocketListener onClientSocketListener;

    public void setOnClientSocketListener(OnClientSocketListener onClientSocketListener) {
        this.onClientSocketListener = onClientSocketListener;
    }

    public ClientSocket(URI serverUri) {
        super(serverUri);
        LogUtils.e(TAG, "ClientSocket " + serverUri.toString());
    }

    @Override
    public void onOpen(ServerHandshake handshakedata) {
        LogUtils.e(TAG, "onOpen 连接成功");

        if (onClientSocketListener != null) {
            onClientSocketListener.onLogging("连接到服务端成功");
        }
    }

    @Override
    public void onMessage(String message) {
        LogUtils.e(TAG, "onMessage 收到消息: " + message);

        if (onClientSocketListener != null) {
            onClientSocketListener.onLogging("收到服务端发来的消息" + message);
        }
    }

    @Override
    public void onClose(int code, String reason, boolean remote) {
        LogUtils.e(TAG, "onClose 连接被" + (remote ? "remote peer" : "us") + "关闭 " + " Code: " + code + " Reason: " + reason);

        if (onClientSocketListener != null) {
            onClientSocketListener.onLogging("断开了  连接被" + (remote ? "remote peer" : "us") + "关闭 " + " Code: " + code + " Reason: " + reason);
        }
    }

    @Override
    public void onError(Exception ex) {
        ex.printStackTrace();
        LogUtils.e(TAG, "onError " + ex.getMessage());
        if (onClientSocketListener != null) {
            onClientSocketListener.onLogging("发生异常" + ex.getMessage());
        }
    }

use location

         URI uri = new URI("ws://" + NetworkUtils.getServerAddressByWifi() + ":3333");
                        clientSocket = new ClientSocket(uri);

                        clientSocket.setSocketFactory(SocketFactory.getDefault());
                        clientSocket.connect();

1691568603978 The result from the picture show that there is a client has successfuly connected, but i didn't see any error from my client.

Can you give me an idea about this problem? Thanks a lot.

yangman92 commented 1 year ago

In addition, i also code a server. I used two as a server and a client. it worked , both successded and can sent message to each.

But i need to connect the tool that is our device.

server coding


public class ServerSocket extends WebSocketServer {

    private static final String TAG = "Socket - ServerSocket";

    private OnServerSocketListener onServerSocketListener;

    public void setOnServerSocketListener(OnServerSocketListener onServerSocketListener) {
        this.onServerSocketListener = onServerSocketListener;
    }

    public ServerSocket(int port) throws UnknownHostException {
        super(new InetSocketAddress(port));
    }

    public ServerSocket(InetSocketAddress address) {
        super(address);

        LogUtils.i(TAG, "ServerSocket" + address.getHostString());
    }

    public ServerSocket(int port, Draft_6455 draft) {
        super(new InetSocketAddress(port), Collections.<Draft>singletonList(draft));
    }

    @Override
    public void onOpen(WebSocket conn, ClientHandshake handshake) {
        LogUtils.i(TAG, "onOpen 加入连接" + conn.getRemoteSocketAddress().getAddress().getHostAddress());

        if (onServerSocketListener != null) {
            onServerSocketListener.onLogging(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " 连接到服务端");
        }
    }

    @Override
    public void onClose(WebSocket conn, int code, String reason, boolean remote) {
        LogUtils.i(TAG, "onClose 断开连接" + conn.getRemoteSocketAddress().getAddress().getHostAddress());
        if (onServerSocketListener != null) {
            onServerSocketListener.onLogging(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " 断开链接");
        }
    }

    @Override
    public void onMessage(WebSocket conn, String message) {
        LogUtils.i(TAG, "onMessage 收到信息" + conn.getRemoteSocketAddress().getAddress().getHostAddress() + message);
        if (onServerSocketListener != null) {
            onServerSocketListener.onLogging(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " 发送了消息 : " + message);
        }
    }

    @Override
    public void onError(WebSocket conn, Exception ex) {
        ex.printStackTrace();
        LogUtils.i(TAG, "onError " + ex.getMessage());

        if (onServerSocketListener != null) {
            onServerSocketListener.onLogging("服务端异常" + ex.getMessage());
        }
    }

    @Override
    public void onStart() {
        LogUtils.i(TAG, "onStart " + getAddress().toString());

        if (onServerSocketListener != null) {
            onServerSocketListener.onLogging("服务端启动 " + getAddress().toString());
        }
    }
}

where i used to

    server = new ServerSocket(new InetSocketAddress(3333));
                    server.start();
marci4 commented 3 months ago

I dont see any real cause in the provided code. But since the server side is not provided, I cannot help and guess.

Closing this issue therefore as cannot reproduce.