STOMP Over WebSocket原文

STOMP是一种简单文本导向的消息协议,适用于多种语言和平台的交互。WebSocket提供了浏览器与服务器之间的双向通信,简化了编程模型。该文章介绍了STOMP与WebSocket的结合使用,包括协议支持、服务器要求、客户端创建、连接、心跳、发送和接收消息等功能,并提供了一个示例。支持的服务器包括HornetQ、ActiveMQ、RabbitMQ等。
摘要由CSDN通过智能技术生成

What is STOMP?

STOMP is a simple text-orientated messaging protocol. It defines an interoperable wire format so that any of the available STOMP clients can communicate with any STOMP message broker to provide easy and widespread messaging interoperability among languages and platforms (the STOMP web site has a list of STOMP client and server implementations.

What is the WebSocket API?


WebSockets are "TCP for the Web".

When Google announced the availability ofWebSocket in Google Chrome,it explained the idea behind WebSockets:

The WebSocket API enables web applications to handle bidirectional communications with server-side process in a straightforward way. Developers have been using XMLHttpRequest ("XHR") for such purposes, but XHR makes developing web applications that communicate back and forth to the server unnecessarily complex. XHR is basically asynchronous HTTP, and because you need to use a tricky technique like long-hanging GET for sending data from the server to the browser, simple tasks rapidly become complex. As opposed to XMLHttpRequest, WebSockets provide a real bidirectional communication channel in your browser. Once you get a WebSocket connection, you can send data from browser to server by calling a send() method, and receive data from server to browser by an onmessage event handler.

In addition to the new WebSocket API, there is also a new protocol (the "WebSocket Protocol") that the browser uses to communicate with servers. The protocol is not raw TCP because it needs to provide the browser's "same-origin" security model. It's also not HTTP because WebSocket traffic differers from HTTP's request-response model. WebSocket communications using the new WebSocket protocol should use less bandwidth because, unlike a series of XHRs and hanging GETs, no headers are exchanged once the single connection has been established. To use this new API and protocol and take advantage of the simpler programming model and more efficient network traffic, you do need a new server implementation to communicate with.

The API is part of HTML5 and is supported (at various degree...) by most modern Web Browsers (including Google Chrome, Firefox and Safari on Mac OS X and iOS).

Protocol Support

This library supports multiple version of STOMP protocols:

Server Requirements

This library is not a pure STOMP client. It is aimed to run on the WebSockets protocol which is not TCP. Basically, the WebSocket protocol requires a handshake between the browser's client and the server to ensure the browser's "same-origin" security model remains in effect.

This means that this library can not connect to regular STOMP brokers since they would not understand the handshake initiated by the WebSocket which is not part of the STOMP protocol and would likely reject the connection.

There are ongoing works to add WebSocket support to STOMP broker so that they will accept STOMP connections over the WebSocket protocol.

HornetQ

HornetQ is the Open Source messaging system developed by Red Hat and JBoss.

To start HornetQ with support for STOMP Over WebSocket, download the latest version and run the following steps:

  $ cd hornetq-x.y.z/examples/jms/stomp-websockets
  $ mvn clean install
  ...
  INFO: HQ221020: Started Netty Acceptor version 3.6.2.Final-c0d783c localhost:61614 for STOMP_WS protocol
  Apr 15, 2013 1:15:33 PM org.hornetq.core.server.impl.HornetQServerImpl$SharedStoreLiveActivation run
  INFO: HQ221007: Server is now live
  Apr 15, 2013 1:15:33 PM org.hornetq.core.server.impl.HornetQServerImpl start
  INFO: HQ221001: HornetQ Server version 2.3.0.CR2 (black'n'yellow2, 123) [c9e29e45-a5bd-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值