src/stomp.nim
changeset 6 7d977f308c75
parent 4 2f4e88604125
child 7 9c5ce539b081
equal deleted inserted replaced
5:e34aabaefe44 6:7d977f308c75
   279     ##    var socket = newSocket()
   279     ##    var socket = newSocket()
   280     ##    let sslContext = newContext( verifyMode = CVerifyNone )
   280     ##    let sslContext = newContext( verifyMode = CVerifyNone )
   281     ##    sslContext.wrapSocket(socket)
   281     ##    sslContext.wrapSocket(socket)
   282     ##    var stomp = newStompClient( socket, "stomp+ssl://test:test@example.com/%2Fvhost" )
   282     ##    var stomp = newStompClient( socket, "stomp+ssl://test:test@example.com/%2Fvhost" )
   283     ##
   283     ##
       
   284 
       
   285     let
       
   286         uri   = parse_uri( uri )
       
   287         vhost = if uri.path.len > 1: uri.path.strip( chars = {'/'}, trailing = false ) else: uri.path
       
   288 
   284     new( result )
   289     new( result )
   285     result.socket        = s
   290     result.socket        = s
   286     result.connected     = false
   291     result.connected     = false
   287     result.uri           = parse_uri( uri )
   292     result.uri           = uri
   288     result.username      = result.uri.username
   293     result.username      = uri.username
   289     result.password      = result.uri.password
   294     result.password      = uri.password
   290     result.host          = result.uri.hostname
   295     result.host          = uri.hostname
   291     result.vhost         = result.uri.path
   296     result.vhost         = vhost
   292     result.timeout       = 500
   297     result.timeout       = 500
   293     result.subscriptions = @[]
   298     result.subscriptions = @[]
   294     result.transactions  = @[]
   299     result.transactions  = @[]
   295 
   300 
   296     # Parse any supported options in the query string.
   301     # Parse any supported options in the query string.
   453 
   458 
   454 
   459 
   455 proc subscribe*( c: StompClient,
   460 proc subscribe*( c: StompClient,
   456             destination: string,
   461             destination: string,
   457             ack        = "auto",
   462             ack        = "auto",
   458             headers:     seq[ tuple[name: string, value: string] ] = @[] ): void =
   463             id:        string = "",
       
   464             headers:   seq[ tuple[name: string, value: string] ] = @[] ): void =
   459     ## Subscribe to messages at **destination**.
   465     ## Subscribe to messages at **destination**.
   460     ##
   466     ##
   461     ## Setting **ack** to "client" or "client-individual" enables client ACK/NACK mode.
   467     ## Setting **ack** to "client" or "client-individual" enables client ACK/NACK mode.
   462     ## In this mode, incoming messages aren't considered processed by
   468     ## In this mode, incoming messages aren't considered processed by
   463     ## the server unless they receive ACK.  By default, the server
   469     ## the server unless they receive ACK.  By default, the server
   466     ## You may optionally add any additional **headers** the server may support.
   472     ## You may optionally add any additional **headers** the server may support.
   467 
   473 
   468     if not c.connected: raise newException( StompError, "Client is not connected." )
   474     if not c.connected: raise newException( StompError, "Client is not connected." )
   469     c.socksend( "SUBSCRIBE" & CRLF )
   475     c.socksend( "SUBSCRIBE" & CRLF )
   470     c.socksend( "destination:" & destination & CRLF )
   476     c.socksend( "destination:" & destination & CRLF )
   471     c.socksend( "id:" & $c.subscriptions.len & CRLF )
   477 
       
   478     if id == "":
       
   479         c.socksend( "id:" & $c.subscriptions.len & CRLF )
       
   480     else:
       
   481         c.socksend( "id:" & id & CRLF )
       
   482 
   472     if ack == "client" or ack == "client-individual":
   483     if ack == "client" or ack == "client-individual":
   473         c.socksend( "ack:" & ack & CRLF )
   484         c.socksend( "ack:" & ack & CRLF )
   474     else:
   485     else:
   475         if ack != "auto": raise newException( StompError, "Unknown ack type: " & ack )
   486         if ack != "auto": raise newException( StompError, "Unknown ack type: " & ack )
   476 
   487 
   657 #
   668 #
   658 # then run another process, to publish stuff:
   669 # then run another process, to publish stuff:
   659 #   ./stomp publisher [stomp-uri] [publish-destination]
   670 #   ./stomp publisher [stomp-uri] [publish-destination]
   660 #
   671 #
   661 # An example with an AMQP "direct" exchange, and an exclusive queue:
   672 # An example with an AMQP "direct" exchange, and an exclusive queue:
   662 #   ./stomp publisher stomp://test:test@localhost/?heartbeat=10 /exchange/test
   673 #   ./stomp publisher stomp://test:test@localhost/%2F?heartbeat=10 /exchange/test
   663 #   ./stomp receiver  stomp://test:test@localhost/?heartbeat=10 /exchange/test
   674 #   ./stomp receiver  stomp://test:test@localhost/%2F?heartbeat=10 /exchange/test
   664 #
   675 #
   665 # Then just let 'er run.
   676 # Then just let 'er run.
   666 #
   677 #
   667 # You can also run a nieve benchmark (deliveries/sec):
   678 # You can also run a naive benchmark (deliveries/sec):
   668 #
   679 #
   669 #   ./stomp benchmark stomp://test:test@localhost/ /exchange/test
   680 #   ./stomp benchmark stomp://test:test@localhost%2F /exchange/test
   670 #
   681 #
   671 # It will set messages to require acknowledgement, and nack everything, causing
   682 # It will set messages to require acknowledgement, and nack everything, causing
   672 # a delivery loop for 10 seconds.
   683 # a delivery loop for 10 seconds.
   673 #
   684 #
   674 when isMainModule:
   685 when isMainModule:
   683 
   694 
   684 then run another process, to publish stuff:
   695 then run another process, to publish stuff:
   685   ./stomp publisher [stomp-uri] [publish-destination]
   696   ./stomp publisher [stomp-uri] [publish-destination]
   686 
   697 
   687 An example with an AMQP "direct" exchange, and an exclusive queue:
   698 An example with an AMQP "direct" exchange, and an exclusive queue:
   688   ./stomp publisher stomp://test:test@localhost/?heartbeat=10 /exchange/test
   699   ./stomp publisher stomp://test:test@localhost/%2F?heartbeat=10 /exchange/test
   689   ./stomp receiver  stomp://test:test@localhost/?heartbeat=10 /exchange/test
   700   ./stomp receiver  stomp://test:test@localhost/%2F?heartbeat=10 /exchange/test
   690 
   701 
   691 Then just let 'er run.
   702 Then just let 'er run.
   692 
   703 
   693 You can also run a nieve benchmark (deliveries/sec):
   704 You can also run a naive benchmark (deliveries/sec):
   694 
   705 
   695   ./stomp benchmark stomp://test:test@localhost/ /exchange/test
   706   ./stomp benchmark stomp://test:test@localhost/%2F /exchange/test
   696 
   707 
   697 It will set messages to require acknowledgement, and nack everything, causing
   708 It will set messages to require acknowledgement, and nack everything, causing
   698 a delivery loop for 10 seconds.
   709 a delivery loop for 10 seconds.
       
   710 If your vhost requires slashes, use URI escaping: /%2Ftest
   699 """
   711 """
   700 
   712 
   701 
   713 
   702     if paramCount() != 3: quit usage
   714     if paramCount() != 3: quit usage
   703 
   715