Skip to content

Commit

Permalink
Improve tunnel (#811)
Browse files Browse the repository at this point in the history
  • Loading branch information
vicancy authored Nov 7, 2024
1 parent 078fe99 commit 780a111
Show file tree
Hide file tree
Showing 7 changed files with 2,974 additions and 2,678 deletions.
9 changes: 5 additions & 4 deletions sdk/server-proxies/src/tunnels/TunnelConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ export class TunnelConnection {
}
const url = this.getUrl(target, this.hub);
const client = new WebPubSubTunnelClient(url, this.credential, this.id, target.target);
logger.info(`Starting connection: ${client.id}, hub: ${this.hub}`);
logger.verbose(`Starting connection: ${client.id}, hub: ${this.hub}`);
client.on("stop", () => {
logger.warning(`Client ${client.getPrintableIdentifier()} stopped, hub: ${this.hub}`);
this.tryEndLife(client.id);
Expand All @@ -345,7 +345,8 @@ export class TunnelConnection {
}
try {
await client.startAsync(abortSignal);
logger.info(`Connected connections: (${this.clients.size})\n` + Array.from(this.printClientLines()).join("\n"));
logger.info(`Connected connections: (${this.clients.size})`);
logger.verbose(Array.from(this.printClientLines()).join("\n"));
return client.id;
} catch (err) {
retryAttempt++;
Expand All @@ -372,7 +373,7 @@ export class TunnelConnection {

private *printClientLines(): IterableIterator<string> {
for (const [clientId, client] of this.clients) {
yield `${client.getPrintableIdentifier()}: connectionId: ${client.currentConnectionId}; userId: ${client.userId}; ended: ${
yield `\t${client.getPrintableIdentifier()}: connectionId: ${client.currentConnectionId}; userId: ${client.userId}; ended: ${
client.stopped
}; target: ${client.target ?? "<random>"}; `;
}
Expand All @@ -384,7 +385,7 @@ export class TunnelConnection {
if (target.endpoint) {
endpoint = target.endpoint;
} else {
logger.info(`No endpoint specified, use original endpoint ${this.endpoint}`);
logger.verbose(`No endpoint specified, use the original endpoint ${this.endpoint}`);
endpoint = this.endpoint;
}
const uriBuilder = new URL(endpoint);
Expand Down
1,732 changes: 779 additions & 953 deletions sdk/server-proxies/yarn.lock

Large diffs are not rendered by default.

36 changes: 22 additions & 14 deletions tools/awps-tunnel/client/public/sample.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# JavaScript

**0. Prerequisites:**

In Azure portal, configure your hub settings to set upstream URL to `tunnel:///eventhandler`.

**1. Install the package:**

```bash
Expand All @@ -10,7 +15,7 @@ npm install @azure/web-pubsub-express
```js
const app = express();
const handler = new WebPubSubEventHandler(hub, {
path: path,
path: "/eventhandler",
handleConnect(_, res) {
console.log(`Connect triggered`);
res.success();
Expand Down Expand Up @@ -38,6 +43,10 @@ app.listen(8080, () => {});

# C#

**0. Prerequisites:**
* In Azure portal, configure your hub settings to set upstream URL to `tunnel:///eventhandler`.
* [ASP.NET Core 8](https://learn.microsoft.com/aspnet/core)

**1. Install the package:**

```bash
Expand All @@ -46,6 +55,8 @@ dotnet add package Microsoft.Azure.WebPubSub.AspNetCore

**2. Use `MapWebPubSubHub`**

The below sample shows how to handle hub `chat` (hub is case-insensitive). Don't forget to rename the `Chat` class to your hub name.

In `Program.cs`:

```csharp
Expand All @@ -57,36 +68,33 @@ builder.Services.AddWebPubSub();

var app = builder.Build();

app.UseRouting();

app.UseEndpoints(endpoints =>
{
endpoints.MapWebPubSubHub<Chat>("/eventhandler/{*path}");
});
app.MapWebPubSubHub<Chat>("/eventhandler/{*path}");

app.Run();

class Chat : WebPubSubHub
{
public override async ValueTask<ConnectEventResponse> OnConnectAsync(ConnectEventRequest request, CancellationToken cancellationToken)
public override ValueTask<ConnectEventResponse> OnConnectAsync(ConnectEventRequest request, CancellationToken cancellationToken)
{
Console.WriteLine("Connect triggered");
return new ConnectEventResponse();
return new ValueTask<ConnectEventResponse>(new ConnectEventResponse());
}

public override async ValueTask<UserEventResponse> OnMessageReceivedAsync(UserEventRequest request, CancellationToken cancellationToken)
public override ValueTask<UserEventResponse> OnMessageReceivedAsync(UserEventRequest request, CancellationToken cancellationToken)
{
return request.CreateResponse(request.Data, request.DataType);
return new ValueTask<UserEventResponse>(request.CreateResponse(request.Data, request.DataType));
}

public override async Task OnConnectedAsync(ConnectedEventRequest request)
public override Task OnConnectedAsync(ConnectedEventRequest request)
{
Console.WriteLine("Connected triggered");
return Task.CompletedTask;
}

public override async Task OnDisconnectedAsync(DisconnectedEventRequest request)
public override Task OnDisconnectedAsync(DisconnectedEventRequest request)
{
Console.WriteLine("Diesconnected triggered");
Console.WriteLine("Disconnected triggered");
return Task.CompletedTask;
}
}
```
Expand Down
6 changes: 6 additions & 0 deletions tools/awps-tunnel/server/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [1.0.0-beta.11] - 2024-11-06
### Improved
- Improve upstream sample code for net8
- Support optional scheme for -u upstream url
- Demote connection logs to verbose

## [1.0.0-beta.10] - 2024-09-21
### Improved
- Some improvements to the REST API tab
Expand Down
1 change: 1 addition & 0 deletions tools/awps-tunnel/server/commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ function createRunCommand(run: Command, dbFile: string, settings: Settings, comm
return;
}
upstream = parsed;
currentUpstream = parsed.toString();
} else {
printer.status(`Upstream is not specified. http://localhost:3000 is used as the default upstream value. Use -u|--upstream to specify the upstream URL.`);
currentUpstream = "http://localhost:3000";
Expand Down
Loading

0 comments on commit 780a111

Please sign in to comment.