// Buffers
static final int DEFAULT_BUFFER_SIZE = Integer.getInteger(
"org.apache.tomcat.websocket.DEFAULT_BUFFER_SIZE", 8 * 1024)
.intValue();
private volatile int maxBinaryMessageBufferSize = Constants.DEFAULT_BUFFER_SIZE;
private volatile int maxTextMessageBufferSize = Constants.DEFAULT_BUFFER_SIZE;
如果发送超过8k的消息,就会报错:文章来源:https://www.toymoban.com/news/detail-516254.html
No async message support and buffer too small Buffer size: [8,192], Message size: [10,240]
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import org.springframework.web.util.WebAppRootListener;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
/**
* 设置websocket 传输内容的字节大小。 默认8k 当前设置50M
*/
@EnableWebSocket
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class WebSocketConfig implements ServletContextInitializer {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.addListener(WebAppRootListener.class);
servletContext.setInitParameter("org.apache.tomcat.websocket.textBufferSize","52428800");
servletContext.setInitParameter("org.apache.tomcat.websocket.binaryBufferSize","52428800");
}
}
@OnMessage(maxMessageSize=5242880)
public void onMessage(String message, Session session, @PathParam("deviceNo")String deviceNo) {
log.info("message:"+message +" ;deviceNo"+deviceNo);
try {
JSONObject obj = JSONObject.parseObject(message);
String funid = (String)obj.get("funid");
JSONObject contentObj = (JSONObject)obj.get("content");
}
} catch (Exception e) {
e.printStackTrace();
}
}
第二种方式在500个连接情况下经过测试不会造成内存溢出文章来源地址https://www.toymoban.com/news/detail-516254.html
到了这里,关于Java实现websocket发送长文本问题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!