stomp.coffee (original) (raw)

All STOMP protocol is exposed as methods of this class (connect(),send(), etc.)
class Client
constructor: (@ws) ->
@ws.binaryType = "arraybuffer"

By default, debug messages are logged in the window’s console if it is defined. This method is called for every actual transmission of the STOMP frames over the WebSocket.
It is possible to set a debug(message) method on a client instance to handle differently the debug messages:

client.debug = function(str) {  
    // append the debug log to a #debug div  
    $("#debug").append(str + "\n");  
};  

debug: (message) ->
window?.console?.log message

heart-beat: sx, sy  
[serverOutgoing, serverIncoming] = (parseInt(v) for v in headers['heart-beat'].split(","))  
unless @heartbeat.outgoing == 0 or serverIncoming == 0  
  ttl = Math.max(@heartbeat.outgoing, serverIncoming)  
  @debug? "send PING every #{ttl}ms"
client.onreceipt = function(frame) {  
  receiptID = frame.headers['receipt-id'];  
  ...  
}  
      when "RECEIPT"  
        @onreceipt?(frame)
var subscription = client.subscribe(destination, onmessage);  
...  
subscription.unsubscribe();  

unsubscribe: (id) ->
delete @subscriptions[id]
@_transmit "UNSUBSCRIBE", {
id: id
}

var tx = client.begin(txid);  
...  
tx.commit();  

commit: (transaction) ->
@_transmit "COMMIT", {
transaction: transaction
}

var tx = client.begin(txid);  
...  
tx.abort();  

abort: (transaction) ->
@_transmit "ABORT", {
transaction: transaction
}

client.subscribe(destination,  
  function(message) {  
    // process the message  
    // acknowledge it  
    message.ack();  
  },  
  {'ack': 'client'}  
);  

ack: (messageID, subscription, headers = {}) ->
headers["message-id"] = messageID
headers.subscription = subscription
@_transmit "ACK", headers

client.subscribe(destination,  
  function(message) {  
    // process the message  
    // an error occurs, nack it  
    message.nack();  
  },  
  {'ack': 'client'}  
);  

nack: (messageID, subscription, headers = {}) ->
headers["message-id"] = messageID
headers.subscription = subscription
@_transmit "NACK", headers

Stomp.WebSocketClass = MozWebSocket  

prior to call Stomp.client().
This hack is deprecated and Stomp.over() method should be used instead.
klass = Stomp.WebSocketClass || WebSocket
ws = new klass(url, protocols)
new Client ws