[Simple filter algorithm] Common filtering algorithm explanation and routine

2023-01-30  

package com.cdr.wsc;

import java.net.URI;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.java_websocket.client.DefaultSSLWebSocketClientFactory;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;

abstract class SimpleWss extends WebSocketClient {

    public SimpleWss(URI serverURI) {
        super(serverURI);
        if(serverURI.toString().contains("wss://"))
            trustAllHosts(this);
    }

    public SimpleWss(URI serverURI,Draft draft) {
        super(serverURI,draft);
        if(serverURI.toString().contains("wss://"))
            trustAllHosts(this);
    }

    final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };


    static void trustAllHosts(SimpleWss appClient) {
        System.out.println("wss");
        TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new java.security.cert.X509Certificate[]{};
            }

            @Override
            public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                // TODO Auto-generated method stub

            }

            @Override
            public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                // TODO Auto-generated method stub

            }
        }};

        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            appClient.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sc));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The use of Simplewss encapsulated here is basically consistent with the usage of WebSocketClient, such as::

import java.net.URI;
import java.net.URISyntaxException;

import org.java_websocket.handshake.ServerHandshake;

public class EscTest {
    public static String initmsg = "{'cmd':'login','data':{'username':'xinji','password':'PASSWORD'}}"
            .replaceAll("'", "\"").replaceAll("PASSWORD", "1402");
    public static void main(String[] args) throws URISyntaxException {
        new SimpleWss(new URI("wss://tjpu.edu.org/websocket")) {

            @Override
            public void onClose(int arg0, String arg1, boolean arg2) {
                System.out.println("onClose");
            }

            @Override
            public void onError(Exception arg0) {
                System.out.println("onError");

            }

            @Override
            public void onMessage(String arg0) {
                System.out.println("onMessage");
                this.send(arg0);
            }

            @Override
            public void onOpen(ServerHandshake arg0) {
                System.out.println("onOpen");
                this.send(initmsg);
            }}.connect();
    }
}

source

Related Posts

Some solutions to Javaweb Chinese garbled Chinese garbled

javascript initialize the SELECT control and set the default selection item

NDK dynamic link library compilation and call for Instrument

Use Settimeout & Window.settimeout with parameters in the JavaScript class (parameters can be object)

[Simple filter algorithm] Common filtering algorithm explanation and routine

Random Posts

tensorflow programming model

Multi -component VOB’s root directory cannot add directory or files. Ivy

Simple SetContentOffset, SetContentsize, and several simple animation use J

MATLAB random number generation method

spark2.0 Study