是否可以使用Spring-WebSockets通过WebSockets通过STOMP发送二进制数据? [英] Is it possible to send binary data with STOMP over WebSockets using Spring-WebSockets?

查看:440
本文介绍了是否可以使用Spring-WebSockets通过WebSockets通过STOMP发送二进制数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在春季文档。但是在高速率下性能很差,所以我希望对二进制消息的使用进行分析。

I am able to send and receive JSON with STOMP over WebSockets following spring documentation. However performance is poor at large high rates, so I wish to profile the use of binary messages.


  • Spring-WebSockets 4.0

  • 在Chrome 35中运行的JavaScript客户端

  • stomp.js 1.7.1

  • Spring-WebSockets 4.0
  • JavaScript client running in Chrome 35
  • stomp.js 1.7.1

I使用SimpMessageTemplate和必要的代理中继发送消息 - 请参阅 spring文档

I send messages using SimpMessageTemplate with the necessary broker relay - see spring documentation

@Controller
public class DemoBinaryController {
   @Autowired
   private SimpMessagingtemplate template

   @Scheduled(fixedDelay = 5000)
   public void demo() throws Exception {
      GenericMessage<byte[]> message = new GenericMessage<byte[]>(new byte[]{65,66,67});
      template.send("/app/binarydemo", message);
   }
}



收货



JavaScript客户端使用标准机制使用 stomp.js 接收数据。

var subscription = client.subscribe("/app/binarydemo", new function(message) {
   console.log("RX message", typeof message.body, message.body.length);
});

收到消息,但是作为字符串,控制台输出如下。我期待一些原始类型,例如 ArrayBuffer

Messages are received, but as Strings, with console output as follows. I'm expecting some raw type, like ArrayBuffer

RX message string  3
RX message string  3



我尝试过的东西



我意识到STOMP中的T代表Text,不过Spring文档意味着二进制消息至少可以使用普通的WebSockets, stomp规范也是如此。 / p>

Things I've tried

I realise the T in STOMP stands for Text, however the Spring documentation implies binary messages are possible at least with plain WebSockets, also the stomp specification states


STOMP是基于文本的,但也允许传输二进制
消息。

STOMP is text based but also allows for the transmission of binary messages.


  • 调试发送代码,就我所见,它似乎保持为byte []

  • 接收时调试stomp.js库。当在底层ws.onmessage回调中收到消息时,该消息似乎是一个字符串(stomp-1.7.1.js中的第243行)

  • 很多搜索 - 这似乎是一个罕见的主题信息

  • 查看stomp.js源代码。对二进制文件的唯一引用是ws.binaryType =arraybuffer。

  • Debugging the sending code, and it appears to remain as byte [] for as far as I can see
  • Debugging the stomp.js library whilst receiving. The message appears to be a String when received in the underlying ws.onmessage callback (line 243 in stomp-1.7.1.js)
  • Lots of searching - this seems a rare topic with little information
  • Looking at the stomp.js source code. The only reference to binary is ws.binaryType = "arraybuffer".
  • 更新:我在服务器端做了更多调试。看来org.springframework.web.socket.TextMessage总是在 org.springframework.web.socket.messaging.StompSubProtocolHandler 而不是org.springframework.web .socket.BinaryMessage。我已经针对此 SPR-12301 提出了功能请求

    Update: I've done more debugging on the server side. It seems that org.springframework.web.socket.TextMessage is always used within org.springframework.web.socket.messaging.StompSubProtocolHandler rather than org.springframework.web.socket.BinaryMessage. I've raised a feature request for this SPR-12301

    message = MessageBuilder.withPayload(message.getPayload()).setHeaders(headers).build();
    byte[] bytes = this.stompEncoder.encode((Message<byte[]>) message);
    
    synchronized(session) {
        session.sendMessage(new TextMessage(new String(bytes, UTF8_CHARSET)));
    }
    



    我的问题




    • 采用这种技术组合,这种方法是否可行?

    • 我错过了一些关键步骤吗?

    • 任何人都能指出我的工作实例

    • My question

      • Is this approach possible with this mix of technologies?
      • Am I missing some crucial step?
      • Can anyone point me to a working example
      • 推荐答案

        似乎org.springframework.web.socket.TextMessage总是在org.springframework.web.socket.messaging.StompSubProtocolHandler而不是org中使用.springframework.web.socket.BinaryMessage。我已经为此 SPR-12301 提出了一项功能请求,该请求已被接受。

        It seems that org.springframework.web.socket.TextMessage is always used within org.springframework.web.socket.messaging.StompSubProtocolHandler rather than org.springframework.web.socket.BinaryMessage. I've raised a feature request for this SPR-12301 which has been accepted.

        message = MessageBuilder.withPayload(message.getPayload()).setHeaders(headers).build();
        byte[] bytes = this.stompEncoder.encode((Message<byte[]>) message);
        
        synchronized(session) {
            session.sendMessage(new TextMessage(new String(bytes, UTF8_CHARSET)));
        }
        

        更新


        • SPR-12301 已在4.1.2中发布,但仅添加支持接收二进制消息

        • 提出 SPR-12475 用于发送二进制消息

        • SPR-12301 was delivered in 4.1.2 but only adds support for receiving binary messages
        • Raised SPR-12475 for sending of binary messages

        这篇关于是否可以使用Spring-WebSockets通过WebSockets通过STOMP发送二进制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆