mrniko / netty-socketio

Socket.IO server implemented on Java. Realtime java framework
Apache License 2.0
6.77k stars 1.65k forks source link

This error IndexOutOfBoundsException is reported when receiving binary files #930

Closed kiwi-field closed 11 months ago

kiwi-field commented 1 year ago

The error message is as follows:

image

The front-end browser transmits parameters as shown in the following figure: 企业微信截图_16932070871602

environment:

spring boot version: 2.4.8,netty-socketio version: 2.0.3, java version: 1.8,frontend socketio.js version 4.7.2 The dependent versions are as follows: image

Backend code:

import com.alibaba.fastjson.JSONObject;
import com.corundumstudio.socketio.SocketIOServer;
import lombok.extern.slf4j.Slf4j;
import net.trueland.aitoolkit.voice.baidu.util.BaiduListener;
import net.trueland.aitoolkit.voice.baidu.util.Const;
import net.trueland.aitoolkit.voice.util.WebsocketUtils;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.WebSocket;
import okio.ByteString;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @创建人:kiwi
 * @创建时间:2023/8/24
 * @描述:
 */
@Component
@Slf4j
public class ServerRunner implements CommandLineRunner, DisposableBean {
    @Autowired
    private  SocketIOServer server;
    @Autowired
    private  WebsocketUtils websocketUtils;
    @Autowired
    private OkHttpClient okHttpClient;
    @Autowired
    private BaiduListener baiduListener;

    @Override
    public void run(String... args) throws InterruptedException {

        server.addConnectListener(client -> {
            log.info("和前端websocket建立好连接, sessionId为{}", client.getSessionId());
            // 和百度建立连接
            String url = Const.URI + "?sn=" + UUID.randomUUID().toString();
            Request request = new Request.Builder().url(url).build();
            WebSocket webSocket = okHttpClient.newWebSocket(request, baiduListener);
            websocketUtils.getFrontToBaiduMap().put(client, webSocket);
            websocketUtils.getBaiduToFrontMap().put(webSocket, client);

        });
        server.addDisconnectListener(client -> {
            WebSocket webSocket = websocketUtils.getFrontToBaiduMap().get(client);
            webSocket.close(1000, "前端连接关闭,同步关闭百度链接");
            log.info("和前端websocket断开连接, sessionId为{}", client.getSessionId());
        });

        server.addEventListener("start", JSONObject.class, (client, data, ackSender) -> {
            log.info("收到一个start数据,会话id{},数据{}", client.getSessionId(), data.toJSONString());
            WebSocket webSocket = websocketUtils.getFrontToBaiduMap().get(client);
            // 发送start帧
            org.json.JSONObject params = new org.json.JSONObject();

            params.put("appid", Const.APPID);
            params.put("appkey", Const.APPKEY);

            params.put("dev_pid", Const.DEV_PID);
            params.put("cuid", "self_defined_server_id_like_mac_address");

            params.put("format", "pcm");
            params.put("sample", 16000);

            org.json.JSONObject json = new org.json.JSONObject();
            json.put("type", "START");
            json.put("data", params);
            log.info("send start FRAME:" + json.toString());
            webSocket.send(json.toString());
        });

        server.addEventListener("finish", JSONObject.class, (client, data, ackSender) -> {
            log.info("收到一个start数据,会话id{},数据{}", client.getSessionId(), data.toJSONString());
            WebSocket webSocket = websocketUtils.getFrontToBaiduMap().get(client);
            org.json.JSONObject json = new org.json.JSONObject();
            json.put("type", "FINISH");
            log.info("send FINISH FRAME:" + json.toString());
            webSocket.send(json.toString());
        });

        server.addEventListener("ing", byte[].class, (client, data, ackSender) -> {
            WebSocket webSocket = websocketUtils.getFrontToBaiduMap().get(client);
            ByteString bytesToSend = ByteString.of(data, 0, data.length);
            webSocket.send(bytesToSend);
        });
        server.start();
    }

    @Override
    public void destroy() throws Exception {
        server.stop();
    }
}
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.Transport;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.trueland.aitoolkit.voice.exception.SocketExceptionListener;
import net.trueland.aitoolkit.voice.listener.AuthListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author fengsu
 */
@Configuration
@Slf4j
@RequiredArgsConstructor
public class SocketConfigure {
    @Value("${wss.server.host:0.0.0.0}")
    private String host;
    @Value("${wss.server.port:9093}")
    private Integer port;
    @Value("${wss.server.context:/socket.io}")
    private String context;
    @Autowired
    private AuthListener authListener;
    @Bean
    public SocketIOServer socketIOServer() {
        com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
        config.setHostname(host);
        config.setPort(port);
        //该处可以用来进行身份验证
        config.setTransports(Transport.WEBSOCKET, Transport.POLLING);
        config.setExceptionListener(new SocketExceptionListener());
        config.setOrigin("*");
        return new SocketIOServer(config);
    }

    /**
     * 用于扫描 netty-socketio 注解 比如 @OnConnect、@OnEvent
     */
    @Bean
    public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketServer) {
        return new SpringAnnotationScanner(socketServer);
    }
}

code : 企业微信截图_16932080044560 企业微信截图_16932080463116

kiwi-field commented 1 year ago

@mrniko

kiwi-field commented 1 year ago

已经解决了,前端加了3行代码。 image

改完以后浏览器请求应该是这样的 809e4e5e3648c71d60d7b61c38a7e2be

liangyuanpeng commented 1 year ago

Seem like it's resolved and can be closed.

ram2406 commented 3 months ago

Hello, I repeated the mistake easily: