TooTallNate / Java-WebSocket

A barebones WebSocket client and server implementation written in 100% Java.

Home Page:http://tootallnate.github.io/Java-WebSocket

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Android device link speed is slow

chenqinggang001 opened this issue · comments

The websocket works properly,But it's too slow to create links

Can be seen in the screenshot from the receipt of a request onWebsocketHandshakeReceivedAsServer callback, until the callback onOpen, It took tens of seconds,How can I improve

image image

I found that the cause of the problem was the use of this callback, it was very slow on some devices, I commented out this part of the code and the link speed was normal

@Override
    public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(WebSocket conn, Draft draft, ClientHandshake request) throws InvalidDataException {
        ServerHandshakeBuilder builder = super.onWebsocketHandshakeReceivedAsServer( conn, draft, request );
        Log.d("zb", "WebSocketServer onWebsocketHandshakeReceivedAsServer: " + conn.getLocalSocketAddress() + "  " + conn.getRemoteSocketAddress());
        if (Objects.equals(conn.getLocalSocketAddress().getHostName(), conn.getRemoteSocketAddress().getHostName())) {
            // 判断是否是自己,拒绝自己连接,减少广播数据消耗
            throw new InvalidDataException(CloseFrame.POLICY_VALIDATION, "Not accepted!");
        }
        return builder;
    }

I think that code actually does two DNS lookups, which would certainly make it slow. Look at the documentation for getHostname().

I removed the getHostName method and replaced it with getAddress, and the problem disappeared