netty-websocket-spring-boot-starter是一个基于netty的websocket服务端,目前笔者使用的版本依托于Springboot。官方网址https://github.com/YeautyYE/netty-websocket-spring-boot-starter本文将帮你解决以下问题:ws://www.aaa.com/api/asrws://www.aaa.com/api/tts共用一个JVM,减少不必要的内存开销。当然示例很简单,在官方文档也有说。以下是代码application.yml
spring:
netty-websocket:
host: 0.0.0.0
port: 80 #默认80可不填写
注意事项:prefix = "spring.netty-websocket"需要和配置文件里面的路径一直,这个属性文件查找的前缀,会有一些默认的值。比如path="/",port=80,如果是这些值,在配置文件里可以不写,只写host
RealTimeTTSEndpoint.java负责处理 /api/tts相关业务
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.yeauty.annotation.OnClose;
import org.yeauty.annotation.OnError;
import org.yeauty.annotation.OnMessage;
import org.yeauty.annotation.OnOpen;
import org.yeauty.annotation.ServerEndpoint;
import org.yeauty.pojo.ParameterMap;
import org.yeauty.pojo.Session;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.net.URI;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
@ServerEndpoint(prefix = "spring.netty-websocket",path = "/api/tts")
@Component
@Slf4j
public class RealTimeTTSEndpoint {
}
RealTimeASREndpoint.java负责处理/api/asr相关业务
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.yeauty.annotation.OnBinary;
import org.yeauty.annotation.OnClose;
import org.yeauty.annotation.OnError;
import org.yeauty.annotation.OnEvent;
import org.yeauty.annotation.OnMessage;
import org.yeauty.annotation.OnOpen;
import org.yeauty.annotation.ServerEndpoint;
import org.yeauty.pojo.Session;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@ServerEndpoint(prefix = "spring.netty-websocket",path = "/api/asr")
@Component
@Slf4j
public class RealTimeASREndpoint {
}
来源:https://www.cnblogs.com/passedbylove/p/12061120.html