Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

100% cpu when using SSL #458

Closed
dan335 opened this issue Apr 22, 2017 · 4 comments
Closed

100% cpu when using SSL #458

dan335 opened this issue Apr 22, 2017 · 4 comments
Assignees
Milestone

Comments

@dan335
Copy link

dan335 commented Apr 22, 2017

Connecting to ws:// I see 3% cpu usage. But when using SSL and connecting to wss:// I see 100% cpu usage. I can't reproduce locally so it's hard to know what's going on. I'm also using getSslContext() for a web server so I know it's correct.

       try {
            aSocketServer = new ASocketServer(443);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        
        try {
            SSLContext sslContext = getSslContext();
            if (sslContext != null) {
                aSocketServer.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(sslContext));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
public static SSLContext getSslContext() {
        try {
            final char[] pass = "pass".toCharArray();
            final KeyStore keyStore = KeyStore.getInstance("JKS");
            
            try (final InputStream is = new FileInputStream("file.jks")) {
                keyStore.load(is, pass);
            }
            
            final KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(keyStore, pass);
            final TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            tmf.init(keyStore);
            final SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new java.security.SecureRandom());
            return sc;               
            
        } catch (KeyStoreException | KeyManagementException | UnrecoverableKeyException | NoSuchAlgorithmException | CertificateException | IOException exc) {
		throw new RuntimeException(exc);
	}
    }
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

import org.java_websocket.WebSocket;
import org.java_websocket.framing.Framedata;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

import java.util.Date;


public class AsSocketServer extends WebSocketServer {
    
    public ASocketServer( int port ) throws UnknownHostException {
        super( new InetSocketAddress( port ) );
    }

    public ASocketServer( InetSocketAddress address ) {
        super( address );
    }

    @Override
    public void onOpen( WebSocket conn, ClientHandshake handshake ) {
    }
}
@becast
Copy link
Contributor

becast commented Apr 23, 2017

I submitted a PR that might solve your issue 203470b

@marci4
Copy link
Collaborator

marci4 commented Apr 23, 2017

Hello @dan335,

could you please tell me which version of the lib you are using right now?
It should be 1.3.2.

As it looks like you are using the lib on a normal client and not android, is this right?
Do you know with which clients this problem occurs?

Greetings
marci4

@dan335
Copy link
Author

dan335 commented Apr 24, 2017

The version is 1.3.2. Normal client not android. I was using chrome with plain javascript websockets.

I got rid of SSL so unfortunately I can't test your fix.

@marci4
Copy link
Collaborator

marci4 commented Apr 24, 2017

Hello @dan335
was able to reproduce this issue with https://www.htbridge.com/ssl test suite.

The fix provided by @becast is working.

Going to release it with the new version 1.3.3.

Greetings
marci4

@marci4 marci4 self-assigned this Apr 24, 2017
@marci4 marci4 added this to the Release 1.3.3 milestone Apr 24, 2017
marci4 added a commit to marci4/Java-WebSocket-Dev that referenced this issue Apr 24, 2017
@marci4 marci4 closed this as completed Apr 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants