spring引导套接字io客户端v1不使用ssl发送到socket.io服务器

cwdobuhd  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(387)

我们使用的是socket.io服务器v2.3.0,没有ssl。js前端客户端和spring引导客户端使用socket.io服务器发送和接收消息。
现在,我们正在使用ssl。js前端工作正常,但spring引导客户端不会向socket.io服务器发送任何消息。这是我向socket.io服务器发送消息的源代码。它在没有ssl的情况下工作。我更改了url并为此设置了https。

IO.Options options = new IO.Options();
options.transports = new String[]{"websocket"};
options.reconnectionAttempts = 2;
options.reconnectionDelay = 1000;
options.timeout = 500;

final Socket socket = IO.socket(socketServerURL, options);
socket.on(Socket.EVENT_CONNECT, args1 -> socket.send("hello..."));
socket.on("connected", objects -> System.out.println("Server connected: " + objects[0].toString()));
socket.on("push_data_event", objects -> System.out.println("Server:" + objects[0].toString()));
socket.on("myBroadcast", objects -> System.out.println("Server:" + objects[0].toString()));
socket.connect();
socket.emit("chanel_name", message);

有什么问题?版本如下:socket服务器:2.3.0 socket js客户端:2.3.0 socket io客户端:1.0.0

9fkzdhlc

9fkzdhlc1#

通过添加一个静态类并将套接字的选项传递给此函数,可以解决此问题。它为选项添加了一些参数并解决了问题。

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
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 io.socket.client.IO;
import okhttp3.OkHttpClient;

public class SocketSSL {

    public static OkHttpClient getOkHttpClient() {

        try {

            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, new TrustManager[]{new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                }

                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
            }}, new java.security.SecureRandom());

            OkHttpClient.Builder builder = new OkHttpClient.Builder();

            builder.hostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });

            builder.sslSocketFactory(sc.getSocketFactory(), new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                }

                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
            });

            return builder.build();

        } catch (NoSuchAlgorithmException | KeyManagementException ex) {
            ex.printStackTrace();
        }

        return null;
    }

    public static void set(IO.Options options) {
        OkHttpClient okHttpClient = getOkHttpClient();
        IO.setDefaultOkHttpWebSocketFactory(okHttpClient);
        IO.setDefaultOkHttpCallFactory(okHttpClient);
        options.callFactory = okHttpClient;
        options.webSocketFactory = okHttpClient;
    }
}

源代码更改为以下内容:

IO.Options options = new IO.Options();
 options.transports = new String[]{"websocket"};
 options.reconnectionAttempts = 2;
 options.reconnectionDelay = 1000;
 options.timeout = 500;
 options.rememberUpgrade = true;
 options.secure = true;

 //usage of the class
 SocketSSL.set(options);

 final Socket socket = IO.socket(socketServerURL, options);
 socket.on(Socket.EVENT_CONNECT, args1 -> socket.send("hello..."));
 socket.on("connected", objects -> System.out.println("Server connected: " + objects[0].toString()));
 socket.on("push_data_event", objects -> System.out.println("Server:" + objects[0].toString()));
 socket.on("myBroadcast", objects -> System.out.println("Server:" + objects[0].toString()));

 socket.connect();
 socket.emit("chanel_name", message);

相关问题