From 1aea3f459ac1135d8171a8cb489da948498a210f Mon Sep 17 00:00:00 2001 From: Zach Smith Date: Wed, 15 May 2019 14:53:03 -0700 Subject: [PATCH] Incorporate a series of patches from Zack Smith. - Allow a vhosts to not require a slash (use %2F in the URI, ignore literal '/') - Support setting a custom 'id' subscription --- src/stomp.nim | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/stomp.nim b/src/stomp.nim index 5418c06..b5d7dbc 100644 --- a/src/stomp.nim +++ b/src/stomp.nim @@ -281,14 +281,19 @@ proc newStompClient*( s: Socket, uri: string ): StompClient = ## sslContext.wrapSocket(socket) ## var stomp = newStompClient( socket, "stomp+ssl://test:test@example.com/%2Fvhost" ) ## + + let + uri = parse_uri( uri ) + vhost = if uri.path.len > 1: uri.path.strip( chars = {'/'}, trailing = false ) else: uri.path + new( result ) result.socket = s result.connected = false - result.uri = parse_uri( uri ) - result.username = result.uri.username - result.password = result.uri.password - result.host = result.uri.hostname - result.vhost = result.uri.path + result.uri = uri + result.username = uri.username + result.password = uri.password + result.host = uri.hostname + result.vhost = vhost result.timeout = 500 result.subscriptions = @[] result.transactions = @[] @@ -455,7 +460,8 @@ proc send*( c: StompClient, proc subscribe*( c: StompClient, destination: string, ack = "auto", - headers: seq[ tuple[name: string, value: string] ] = @[] ): void = + id: string = "", + headers: seq[ tuple[name: string, value: string] ] = @[] ): void = ## Subscribe to messages at **destination**. ## ## Setting **ack** to "client" or "client-individual" enables client ACK/NACK mode. @@ -468,7 +474,12 @@ proc subscribe*( c: StompClient, if not c.connected: raise newException( StompError, "Client is not connected." ) c.socksend( "SUBSCRIBE" & CRLF ) c.socksend( "destination:" & destination & CRLF ) - c.socksend( "id:" & $c.subscriptions.len & CRLF ) + + if id == "": + c.socksend( "id:" & $c.subscriptions.len & CRLF ) + else: + c.socksend( "id:" & id & CRLF ) + if ack == "client" or ack == "client-individual": c.socksend( "ack:" & ack & CRLF ) else: @@ -659,14 +670,14 @@ proc wait_for_messages*( c: StompClient, loop=true ) = # ./stomp publisher [stomp-uri] [publish-destination] # # An example with an AMQP "direct" exchange, and an exclusive queue: -# ./stomp publisher stomp://test:test@localhost/?heartbeat=10 /exchange/test -# ./stomp receiver stomp://test:test@localhost/?heartbeat=10 /exchange/test +# ./stomp publisher stomp://test:test@localhost/%2F?heartbeat=10 /exchange/test +# ./stomp receiver stomp://test:test@localhost/%2F?heartbeat=10 /exchange/test # # Then just let 'er run. # -# You can also run a nieve benchmark (deliveries/sec): +# You can also run a naive benchmark (deliveries/sec): # -# ./stomp benchmark stomp://test:test@localhost/ /exchange/test +# ./stomp benchmark stomp://test:test@localhost%2F /exchange/test # # It will set messages to require acknowledgement, and nack everything, causing # a delivery loop for 10 seconds. @@ -685,17 +696,18 @@ then run another process, to publish stuff: ./stomp publisher [stomp-uri] [publish-destination] An example with an AMQP "direct" exchange, and an exclusive queue: - ./stomp publisher stomp://test:test@localhost/?heartbeat=10 /exchange/test - ./stomp receiver stomp://test:test@localhost/?heartbeat=10 /exchange/test + ./stomp publisher stomp://test:test@localhost/%2F?heartbeat=10 /exchange/test + ./stomp receiver stomp://test:test@localhost/%2F?heartbeat=10 /exchange/test Then just let 'er run. -You can also run a nieve benchmark (deliveries/sec): +You can also run a naive benchmark (deliveries/sec): - ./stomp benchmark stomp://test:test@localhost/ /exchange/test + ./stomp benchmark stomp://test:test@localhost/%2F /exchange/test It will set messages to require acknowledgement, and nack everything, causing a delivery loop for 10 seconds. +If your vhost requires slashes, use URI escaping: /%2Ftest """