Skip to content

Commit

Permalink
Merge pull request #39 from ngrok/nikolay/update-quickstart
Browse files Browse the repository at this point in the history
update readme quickstarter
  • Loading branch information
nikolay-ngrok authored Nov 21, 2023
2 parents c824f9a + ea96588 commit 3bf50b3
Showing 1 changed file with 37 additions and 32 deletions.
69 changes: 37 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,40 +109,45 @@ dependencies {
## Quickstart

```java
package com.ngrok.quickstart;

import com.ngrok.Session;
import com.ngrok.HttpTunnel;

public class Echo {
public static void main(String[] args) throws IOException {
// Session.withAuthtokenFromEnv() will create a new session builder, pulling NGROK_AUTHTOKEN env variable.
// You can get your authtoken by registering at https://dashboard.ngrok.com
var sessionBuilder = Session.withAuthtokenFromEnv().metadata("my session");
// Session.Builder let you customize different aspects of the session, see docs for details.
// After customizing the builder, you connect:
try (var session = sessionBuilder.connect()) {
// Creates and configures http listener that will be using oauth to secure it
var listenerBuilder = session.httpEndpoint().metadata("my listener")
.oauth(new Http.OAuthOptions("google")));
// Now start listening with the above configuration
try (var listener = listenerBuilder.listen()) {
System.out.println("ngrok url: " + listener.getUrl());
var buf = ByteBuffer.allocateDirect(1024);

while (true) {
// Accept a new connection
var conn = listener.accept();

// Read from the connection
conn.read(buf);

System.out.println(buf.asCharBuffer());

// Or write to it
conn.write(buf);
import com.ngrok.Http;

import java.io.IOException;
import java.nio.ByteBuffer;

public class Quickstart {
public static void main(String[] args) throws IOException {
// Session.withAuthtokenFromEnv() will create a new session builder, pulling NGROK_AUTHTOKEN env variable.
// You can get your authtoken by registering at https://dashboard.ngrok.com
var sessionBuilder = Session.withAuthtokenFromEnv().metadata("my session");
// Session.Builder let you customize different aspects of the session, see docs for details.
// After customizing the builder, you connect:
try (var session = sessionBuilder.connect()) {
// Creates and configures http listener that will be using oauth to secure it
var listenerBuilder = session.httpEndpoint().metadata("my listener")
.oauthOptions(new Http.OAuth("google"));
// Now start listening with the above configuration
try (var listener = listenerBuilder.listen()) {
System.out.println("ngrok url: " + listener.getUrl());
var buf = ByteBuffer.allocateDirect(1024);

while (true) {
// Accept a new connection
var conn = listener.accept();

// Read from the connection
conn.read(buf);

System.out.println(buf.asCharBuffer());

// Or write to it
conn.write(buf);
}
}
}
}
}
}
}
}
```

Expand Down

0 comments on commit 3bf50b3

Please sign in to comment.