一、概述

STOMP 客户端类
所有 STOMP 协议都公开为此类的方法(connect(), send()等)

二、实例方法总结


2.connect()

  1. ~ (void) connect(headers, connectCallback)
  2. ~ (void) connect(headers, connectCallback, errorCallback)
  3. ~ (void) connect(login, passcode, connectCallback)
  4. ~ (void) connect(login, passcode, connectCallback, errorCallback)
  5. ~ (void) connect(login, passcode, connectCallback, errorCallback, closeEventCallback)
  6. ~ (void) connect(login, passcode, connectCallback, errorCallback, closeEventCallback, host)

注意: 当自动重新连接处于活动状态时,connectCallback将errorCallback在每次连接或错误时调用
该connect方法接受不同数量的参数和类型。请参阅重载列表。使用带有标头的版本来传递您的代理特定选项。
例子:

  1. client.connect('guest, 'guest', function(frame) {
  2. client.debug("connected to Stomp");
  3. client.subscribe(destination, function(message) {
  4. $("#messages").append("<p>" + message.body + "</p>\n");
  5. });
  6. });

参数:

  • headers ( Object )
  • connectCallback ( function(Frame) ) — Called upon a successful connect or reconnect
  • errorCallback ( function(any) ) — Optional, called upon an error. The passed paramer may be a Frame or a message
  • closeEventCallback ( function(CloseEvent) ) — Optional, called when the websocket is closed.
  • login ( String )
  • passcode ( String )
  • host ( String ) — Optional, virtual host to connect to. STOMP 1.2 makes it mandatory, however the broker may not mandate it

Options Hash: (headers):

  • login ( String )
  • passcode ( String )
  • host ( String ) — virtual host to connect to. STOMP 1.2 makes it mandatory, however the broker may not mandate it

See also:

Overloads:

  1. ~ (void) connect(headers, connectCallback)
  2. ~ (void) connect(headers, connectCallback, errorCallback)
  3. ~ (void) connect(login, passcode, connectCallback)
  4. ~ (void) connect(login, passcode, connectCallback, errorCallback)
  5. ~ (void) connect(login, passcode, connectCallback, errorCallback, closeEventCallback)
  6. ~ (void) connect(login, passcode, connectCallback, errorCallback, closeEventCallback, host)

3.disconnect()

  1. # (void) disconnect(disconnectCallback, headers = {})

断开与 STOMP 代理的连接。为了确保正常关闭,它会发送一个 DISCONNECT 帧并等待代理确认。
disconnectCallback 仅在代理实际连接时才会被调用。
参数:

  • disconnectCallback ( function() )
  • headers ( Object ) — optional

See also:


4.send()

  1. # (void) send(destination, headers = {}, body = '')

注意: 正文必须是字符串。您需要将有效负载转换为字符串,以防它不是字符串(例如 JSON)
向指定目的地发送消息。有关目的地的类型和命名,请参阅您的 STOMP 代理文档。标头通常对订阅者可用。但是,可能有与您的 STOMP 代理相对应的特殊用途标头。
Examples:

  1. client.send("/queue/test", {priority: 9}, "Hello, STOMP");

payload without headers

  1. # If you want to send a message with a body, you must also pass the headers argument.
  2. client.send("/queue/test", {}, "Hello, STOMP");

Parameters:

  • destination ( String ) — mandatory
  • headers ( Object ) — Optional
  • body ( String ) — Optional

See also:


5.subscribe()

  1. # (Object) subscribe(destination, callback, headers = {})

注意: 如果标题中没有提供任何 ID,则库将生成唯一 ID。要使用您自己的 ID,请使用 headers 参数传递它
订阅 STOMP 经纪人位置。返回值是一个带有取消订阅方法的对象。
var subscription = client.subscribe(“/queue/test”, callback);
Examples:

  1. callback = function(message) {
  2. // called when the client receives a STOMP message from the server
  3. if (message.body) {
  4. alert("got message with body " + message.body)
  5. } else
  6. {
  7. alert("got empty message");
  8. }
  9. });

Explicit subscription id

  1. var mysubid = 'my-subscription-id-001';
  2. var subscription = client.subscribe(destination, callback, { id: mysubid });

Parameters:

  • destination ( String )
  • callback ( function(message) )
  • headers ( Object ) — optional

Returns:

  • ( Object ) — this object has a method to unsubscribe

See also:


6.unsubscribe()

  1. # (void) unsubscribe(id, headers = {})

最好通过直接对客户端client.subscribe()返回的对象调用unsubscribe()来取消订阅。
Examples:

  1. var subscription = client.subscribe(destination, onmessage);
  2. ...
  3. subscription.unsubscribe();

Parameters:

  • id ( String )
  • headers ( Object ) — optional

See also:


7.begin()

  1. # (Object) begin(transaction_id)

注意: 如果没有传递交易ID,则会自动创建一个
开始一个事务,返回的Object对象有commit和abort方法
Parameters:

  • transaction_id ( String ) — optional

Returns:

  • ( Object ) — member, id - transaction id, methods commit and abort

See also:


8.commit()

  1. # (void) commit(transaction_id)

提交事务。最好通过直接对客户端client.begin()返回的对象调用commit()来提交事务。
Examples:
var tx = client.begin(txid); … tx.commit();
Parameters:

  • transaction_id ( String )

See also:


9.abort()

  1. # (void) abort(transaction_id)

中止事务。最好通过直接对客户端client.begin()返回的对象调用abort()来中止事务。
Examples:
var tx = client.begin(txid); … tx.abort();
Parameters:

  • transaction_id ( String )

See also:


10.ack()

  1. # (void) ack(messageID, subscription, headers = {})

告知收到消息。最好通过直接对订阅回调处理的消息调用ack()来告知收到消息
Examples:

  1. client.subscribe(destination,
  2. function(message) {
  3. // process the message
  4. // acknowledge it
  5. message.ack();
  6. },
  7. {'ack': 'client'}
  8. );

Parameters:

  • messageID ( String )
  • subscription ( String )
  • headers ( Object ) — optional

See also:


11.nack()

  1. # (void) nack(messageID, subscription, headers = {})

告知没有收到消息。最好通过直接对订阅回调处理的消息调用nack()来告知没有收到消息
Examples:

  1. client.subscribe(destination,
  2. function(message) {
  3. // process the message
  4. // an error occurs, nack it
  5. message.nack();
  6. },
  7. {'ack': 'client'}
  8. );

Parameters:

  • messageID ( String )
  • subscription ( String )
  • headers ( Object ) — optional

See also: