mirror of
				https://github.com/taigrr/nats.docs
				synced 2025-01-18 04:03:23 -08:00 
			
		
		
		
	
		
			
				
	
	
		
			143 lines
		
	
	
		
			6.4 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			143 lines
		
	
	
		
			6.4 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
| 
 | |
| <div class="tab-wrap">
 | |
|   	
 | |
| 	
 | |
| 	<input type="radio" id="reconnect_event_go" name="reconnect_event" class="tab" checked>
 | |
| 	
 | |
| 	<label for="reconnect_event_go" class="api-lang" data-language="go">Go</label>
 | |
| 	
 | |
| 	
 | |
| 	<input type="radio" id="reconnect_event_java" name="reconnect_event" class="tab">
 | |
| 	
 | |
| 	<label for="reconnect_event_java" class="api-lang" data-language="java">Java</label>
 | |
| 	
 | |
| 	
 | |
| 	<input type="radio" id="reconnect_event_js" name="reconnect_event" class="tab">
 | |
| 	
 | |
| 	<label for="reconnect_event_js" class="api-lang" data-language="js">JavaScript</label>
 | |
| 	
 | |
| 	
 | |
| 	<input type="radio" id="reconnect_event_py" name="reconnect_event" class="tab">
 | |
| 	
 | |
| 	<label for="reconnect_event_py" class="api-lang" data-language="py">Python</label>
 | |
| 	
 | |
| 	
 | |
| 	<input type="radio" id="reconnect_event_ruby" name="reconnect_event" class="tab">
 | |
| 	
 | |
| 	<label for="reconnect_event_ruby" class="api-lang" data-language="ruby">Ruby</label>
 | |
| 	
 | |
| 	
 | |
| 	<input type="radio" id="reconnect_event_ts" name="reconnect_event" class="tab">
 | |
| 	
 | |
| 	<label for="reconnect_event_ts" class="api-lang" data-language="ts">TypeScript</label>
 | |
| 	
 | |
| 
 | |
|   	
 | |
| 	<div class="tab__content">
 | |
| 	<pre id="reconnect_event_go_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/go-nats-examples/blob/master/api-examples/reconnect_event/main.go#L10-28"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-go">// Connection event handlers are invoked asynchronously
 | |
| // and the state of the connection may have changed when
 | |
| // the callback is invoked.
 | |
| nc, err := nats.Connect("demo.nats.io",
 | |
| 	nats.DisconnectHandler(func(nc *nats.Conn) {
 | |
| 		// handle disconnect event
 | |
| 	}),
 | |
| 	nats.ReconnectHandler(func(nc *nats.Conn) {
 | |
| 		// handle reconnect event
 | |
| 	}))
 | |
| if err != nil {
 | |
| 	log.Fatal(err)
 | |
| }
 | |
| defer nc.Close()
 | |
| 
 | |
| // Do something with the connection
 | |
| 
 | |
| </code></pre>
 | |
| 	</div>
 | |
| 	
 | |
| 	<div class="tab__content">
 | |
| 	<pre id="reconnect_event_java_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/java-nats-examples/blob/master/src/main/java/io/nats/examples/ReconnectEvents.java#L12-28"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-java">Options options = new Options.Builder().
 | |
|                             server("nats://demo.nats.io:4222").
 | |
|                             connectionListener((conn, type) -> {
 | |
|                                 if (type == Events.RECONNECTED) {
 | |
|                                     // handle reconnected
 | |
|                                 } else if (type == Events.DISCONNECTED) {
 | |
|                                     // handle disconnected, wait for reconnect
 | |
|                                 }
 | |
|                             }).
 | |
|                             build();
 | |
| Connection nc = Nats.connect(options);
 | |
| 
 | |
| // Do something with the connection
 | |
| 
 | |
| nc.close();
 | |
| </code></pre>
 | |
| 	</div>
 | |
| 	
 | |
| 	<div class="tab__content">
 | |
| 	<pre id="reconnect_event_js_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/node-nats-examples/blob/master/src/reconnect_samples.js#L74-83"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-javascript">let nc = NATS.connect({
 | |
|     maxReconnectAttempts: 10,
 | |
|     servers: ["nats://demo.nats.io:4222"]
 | |
| });
 | |
| 
 | |
| nc.on('reconnect', (c) => {
 | |
|     console.log('reconnected');
 | |
| });
 | |
| </code></pre>
 | |
| 	</div>
 | |
| 	
 | |
| 	<div class="tab__content">
 | |
| 	<pre id="reconnect_event_py_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/asyncio-nats-examples/blob/master/reconnect_event.py#L6-25"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-python">nc = NATS()
 | |
| 
 | |
| async def disconnected_cb():
 | |
|    print("Got disconnected!")
 | |
| 
 | |
| async def reconnected_cb():
 | |
|    # See who we are connected to on reconnect.
 | |
|    print("Got reconnected to {url}".format(url=nc.connected_url.netloc))
 | |
| 
 | |
| await nc.connect(
 | |
|    servers=["nats://demo.nats.io:4222"],
 | |
|    reconnect_time_wait=10,
 | |
|    reconnected_cb=reconnected_cb,
 | |
|    disconnected_cb=disconnected_cb,
 | |
|    )
 | |
| 
 | |
| # Do something with the connection.
 | |
| 
 | |
| </code></pre>
 | |
| 	</div>
 | |
| 	
 | |
| 	<div class="tab__content">
 | |
| 	<pre id="reconnect_event_ruby_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/ruby-nats-examples/blob/master/reconnect_event.rb#L1-14"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-ruby">require 'nats/client'
 | |
| 
 | |
| NATS.start(servers: ["nats://127.0.0.1:1222", "nats://127.0.0.1:1223", "nats://127.0.0.1:1224"]) do |nc|
 | |
|    # Do something with the connection
 | |
|    nc.on_reconnect do
 | |
|     puts "Got reconnected to #{nc.connected_server}"
 | |
|   end
 | |
| 
 | |
|   nc.on_disconnect do |reason|
 | |
|     puts "Got disconnected! #{reason}"
 | |
|   end
 | |
| end
 | |
| </code></pre>
 | |
| 	</div>
 | |
| 	
 | |
| 	<div class="tab__content">
 | |
| 	<pre id="reconnect_event_ts_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/ts-nats-examples/blob/master/src/reconnect_samples.ts#L57-70"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-javascript">// will throw an exception if connection fails
 | |
| let nc = await connect({
 | |
|     maxReconnectAttempts: 10,
 | |
|     servers: ["nats://demo.nats.io:4222"]
 | |
| });
 | |
| // first argument is the connection (same as nc in this case)
 | |
| // second argument is the url of the server where the client
 | |
| // connected
 | |
| nc.on('reconnect', (conn, server) => {
 | |
|     console.log('reconnected to', server);
 | |
| });
 | |
| nc.close();
 | |
| </code></pre>
 | |
| 	</div>
 | |
| 	
 | |
| </div>
 |