尝试发送大尺寸图像时

尝试发送大尺寸图像时

本文介绍了尝试发送大尺寸图像时 WebSocket 断开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 java 和 javascirpt 在本地主机上测试 WebSocket,运行 Tomcat 7.0.42,中间没有代理.它适用于通过 websocket 发送文本和小尺寸图像.但是,当尝试发送大尺寸照片时,它会强制关闭客户端(chrome 浏览器)的连接(注意,在浏览器上的 websocket 关闭后,不会通知 tomcat 的MessageInbound 中的 onClose 回调"连接).

I am testing a WebSocket at localhost using java and javascirpt, running Tomcat 7.0.42 and no proxy in between. It works fine on sending text and small size of image via websocket. However, it will be forced to close the connection on client side(chrome browser) when trying to send a large size of photo (Notice that the tomcat's 'onClose callback in MessageInbound' does not be notified after websocket on browser closed the connection).

我该如何解决?谢谢.

这是chrome开发工具的截图

以下是我在客户端的代码:

Below is my code on client side:

for (var i = 0, f; f = files[i]; i++) {

    // step 1: tell server who the people you want to send
    ws.send(JSON.stringify({
        action: "binary",
        receiver: <%=selectedfriend.getUserId()%>,
        timestamp: new Date().getTime()
   }));

   // step 2: send file
   ws.send(f);

   var reader = new FileReader();
   reader.onload = (function(theFile) {
       return function(e) {
           var span = document.createElement('span');
           span.innerHTML = ['<img class="thumb" style="width: 50px;height: 30px;" src="', e.target.result,
           '" title="', escape(theFile.name), '"/>'].join('');
           appendImage(span.innerHTML, "pullleft");
       };
   })(f);
   reader.readAsDataURL(f);
}

推荐答案

终于找到了解决我的问题的办法,我在Tomcat上做的就是使用:

Finally, I found a solution for my question, what i did on Tomcat is to use:

    protected StreamInbound createWebSocketInbound(String string, HttpServletRequest hsr) {

    MyMessageInbound inbound = new MyMessageInbound();

    inbound.setByteBufferMaxSize(9999999);
    inbound.setOutboundByteBufferSize(9999999);

    return inbound;
}

但是还有一个问题是:我应该使用的正确值是多少,这里是 9999999,当我使用 WebSocket 上传 8-9MB 文件时应该很好地测试该值,但是为什么,我该如何测量它?请再次在这里帮助和讨论,谢谢!

But there is another problem is:what is the proper value I should use, here is 9999999, the value should be tested well when I upload under 8-9MB file using WebSocket, but why, how can I measure it?Please help and discuss here again, thx!

这篇关于尝试发送大尺寸图像时 WebSocket 断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 18:55