我的代码
import com.google.common.collect.ImmutableMap; import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; import com.google.gson.Gson; import com.xxx.web.open.service.CacheService; import com.xxx.web.open.util.AudioStreamHelper; import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.tuple.ImmutablePair; import org.java_websocket.enums.ReadyState; import org.java_websocket.client.WebSocketClient; import org.java_websocket.handshake.ServerHandshake; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.DigestUtils; import java.io.File; import java.net.URI; import java.nio.ByteBuffer; import java.util.Map; import java.util.Objects; import java.util.UUID; import java.util.concurrent.locks.ReentrantLock; /*** * 离线语音合成 */ @Slf4j @Component("ttsService") public class OfflineTTSService extends WebSocketClient { public String textToSpeech(String text, String speakerId) throws InterruptedException { /*** * 根据请求的内容计算hash值,防止请求重放,产生不必要的请求,浪费服务器资源(内存、cpu、硬盘空间) * hash值产生算法: * 1、byteArrayOf("tts")+byteArrayOf("待转换的文字") * 2、byteArrayOf("asr")+音频流二进制 byte[] * 3、byteArrayOf("merge") + 文件流 byteArray1[] + 文件流byteArray2[] + ... 文件流 byteArrayN[] */ ImmutablePair<Boolean, String> result = findConversion(computeHash(text,speakerId)); Boolean existed = result.getLeft(); if(existed) { String uuid = result.getRight(); log.warn("重复的合成请求:{}",uuid); return uuid; } synchronized (this) { if(getReadyState()==ReadyState.NOT_YET_CONNECTED) { if(isClosed()) { reconnectBlocking(); }else{ connectBlocking(); } }else if(getReadyState() == ReadyState.CLOSED){ reconnectBlocking(); } } }
之前的方法
import com.google.common.collect.ImmutableMap; import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; import com.google.gson.Gson; import com.x.web.open.service.CacheService; import com.x.web.open.util.AudioStreamHelper; import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.tuple.ImmutablePair; import org.java_websocket.enums.ReadyState; import org.java_websocket.client.WebSocketClient; import org.java_websocket.handshake.ServerHandshake; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.DigestUtils; import java.io.File; import java.net.URI; import java.nio.ByteBuffer; import java.util.Map; import java.util.Objects; import java.util.UUID; import java.util.concurrent.locks.ReentrantLock; /*** * 离线语音合成 */ @Slf4j @Component("ttsService") public class OfflineTTSService extends WebSocketClient { public String textToSpeech(String text, String speakerId) throws InterruptedException { /*** * 根据请求的内容计算hash值,防止请求重放,产生不必要的请求,浪费服务器资源(内存、cpu、硬盘空间) * hash值产生算法: * 1、byteArrayOf("tts")+byteArrayOf("待转换的文字") * 2、byteArrayOf("asr")+音频流二进制 byte[] * 3、byteArrayOf("merge") + 文件流 byteArray1[] + 文件流byteArray2[] + ... 文件流 byteArrayN[] */ ImmutablePair<Boolean, String> result = findConversion(computeHash(text,speakerId)); Boolean existed = result.getLeft(); if(existed) { String uuid = result.getRight(); log.warn("重复的合成请求:{}",uuid); return uuid; } synchronized (this) { if(!isOpen()) { connectBlocking(); } } }
经过以上修改解决问题
其他地方调用者服务
@Autowired
OfflineTTSService ttsService;
@Configuration public class WebAppConfig extends WebMvcConfigurerAdapter { @Bean public URI serverUri() throws URISyntaxException { return new URI("ws://xxx.yyy.zzz.aaa:8081/test"); } }
本文标签:Java-WebSocket 调用 报错 WebSocketClient objects are not reuseable
本文链接: https://www.51dev.com/java/36915
版权说明: 原创文章,未经授权请勿转载。