简体   繁体   English

通过Sockjs + Spring Websocket + Stomp发送图像/文件

[英]Sending images/files over Sockjs + Spring Websocket + Stomp

I am working on a messaging application using Spring websockets(STOMP as a sub-protocol) and Sockjs. 我正在使用Spring websockets(STOMP作为子协议)和Sockjs开发消息传递应用程序。

I should provide support to send files in messages. 我应该提供支持来发送消息中的文件。

According to this ticket , sockjs does not support binary data, but STOMP does. 根据这张票 ,sockjs不支持二进制数据,但STOMP确实如此。

I know that we can convert image to base64 and send it over stomp, but i think this is not the best practice as there is lot of conversion and overhead. 我知道我们可以将图像转换为base64并通过stomp发送它,但我认为这不是最好的做法,因为有很多转换和开销。 Also I have to save the messages, So to save this base64 encoded files at server again I will have to decode them. 此外,我必须保存消息,所以要再次在服务器上保存这个base64编码文件我将不得不解码它们。

I have couple of questions : 我有几个问题:

1) Is there a workaround to send image/files over sockjs + stomp or converting to Base64 is the only way? 1)是否有通过sockjs + stomp发送图像/文件转换为Base64的解决方法是唯一的方法?

2) May be this a very silly question but according to this question it is possible to send binary data over STOMP(without sockjs). 2)这可能是一个非常愚蠢的问题,但根据这个问题 ,可以通过STOMP发送二进制数据(没有sockjs)。 How difficult is it to support fallback without sockjs? 没有sockjs支持后备有多难?

Thank you. 谢谢。

EDIT : If using base64 is the only option, I would rather make a POST request to save the messages which has attachments instead of using base64 encoding. 编辑如果使用base64是唯一的选择,我宁愿发出POST请求来保存具有附件的消息,而不是使用base64编码。 Any ideas which is better? 任何更好的想法?

Any Web Socket implementation will handle binary data if it is base64 encoded. 任何Web Socket实现都将处理二进制数据(如果它是base64编码的)。 This essentially serializes a binary stream to a string. 这基本上将二进制流序列化为字符串。 All socket transports and wrappers can handle string data. 所有套接字传输和包装器都可以处理字符串数据。 Any Java base64 implementation should work. 任何Java base64实现都应该有效。

On the browsers side base64 is handled natively in modern browsers with btoa() and atob() . 在浏览器端,base64在现代浏览器中使用btoa()atob() If you support legacy browsers you may need a polyfill. 如果您支持旧版浏览器,则可能需要填充。

That said, if the Java server is just proxying messages between Web users, you won't need to decode the images in Java, you would just pass the string encoded images from one socket connection to another. 也就是说,如果Java服务器只是在Web用户之间代理消息,您将不需要用Java解码图像,您只需将字符串编码图像从一个套接字连接传递到另一个套接字连接。

You have a look binaryjs . 你看看binaryjs。 it is promise to Realtime binary streaming for the web using websockets. 它承诺使用websockets实现Web的实时二进制流。

lets check the examples 让我们看看这些例子

https://github.com/binaryjs/binaryjs/tree/master/examples https://github.com/binaryjs/binaryjs/tree/master/examples

You should check Atmosphere(wasync) , link will guide you to how to implement. 你应该检查Atmosphere(wasync) ,链接将指导你如何实现。

as far as understood your browser fallback scenario on client side you can use [base64 polyfill lib ( https://github.com/davidchambers/Base64.js/ ) . 据了解,客户端的浏览器回退场景可以使用[base64 polyfill lib( https://github.com/davidchambers/Base64.js/ )。

Best Regards 最好的祝福

Another alternative would be to use WebRTC only for sending of the binary images. 另一种选择是仅使用WebRTC发送二进制图像。 One of the advanges to this is that these messages would be purely peer-to-peer. 对此的一个优点是这些消息纯粹是点对点的。 You would only need to implement this in your Web app and not in your backend. 您只需要在Web应用程序中实现此功能,而不是在后端。 You would not then need to pay for the bandwidth used for the images. 然后,您不需要为图像使用的带宽付费。 Unfortunately though, WebRTC is not supported by IE. 不幸的是,IE不支持WebRTC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM