Websocket File upload speed issue (Java websocket API and Javascript)

本小妞迷上赌 提交于 2019-12-04 13:41:59

Try to put setMaxBinaryMessageBufferSize() to 1MB to your session. Don't know exactly with javax but with jetty it does not changed the fragments size but the speed is increased enormously.

The session object has many methods. Some of them are introduced to deal with your problem.

setMaxBinaryMessageBufferSize(int lenght) - because it seems that your client sends the entire file data at once, the the buffer on the server is unable to handle that size.

In reality, sending the whole file data makes the program vulnerable and dependent on the file it processes. It is also not a good performance decision. Because you mention that your process works quite slow. I general, allocating the buffer size large enough to be able to hold the file data in memory could degrade performance because it could cause memory swapping.

I would upload large files by multiple segments. I did this using some socket APIs (not WebSocket API) and it work quite fast.

Another point. I noticed that you are using two OnMessage methods for different message types. However, I think you need to use in that case MessageHandlers, otherwise your program, as you reported, can confuse which message go to which method.

Please see this example https://github.com/nickytd/websocket-message-handlers-example/blob/master/websocket.messagehandler.web/src/main/java/websocket/messagehandler/example/endpoints/FullEchoEndpoint.java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!