WebSocket 자바 클 라 이언 트 요청 WSS

3501 단어 websocket
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();
        }
    }
}

여기에 포 장 된 Simple Wss 와 WebSocketClient 의 용법 은 기본적으로 일치 합 니 다. 예 를 들 어:
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();
    }
}

좋은 웹페이지 즐겨찾기