Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,12 @@ Generated spec:
Generated TypeScript code:

```typescript
import { HubConnection, IStreamResult } from "@aspnet/signalr"
import { HubConnection, IStreamResult } from '@aspnet/signalr'

export class ChatHub {
callbackForWelcome: () => void;
callbackForSend: (message: string) => void;

constructor(private connection: HubConnection) {
}

Expand All @@ -192,13 +195,15 @@ export class ChatHub {
}

registerCallbacks(implementation: IChatHubCallbacks) {
this.connection.on('Welcome', () => implementation.welcome());
this.connection.on('Send', (message) => implementation.send(message));
this.callbackForWelcome = () => implementation.welcome();
this.connection.on('Welcome', this.callbackForWelcome);
this.callbackForSend = (message) => implementation.send(message);
this.connection.on('Send', this.callbackForSend);
}

unregisterCallbacks(implementation: IChatHubCallbacks) {
this.connection.off('Welcome', () => implementation.welcome());
this.connection.off('Send', (message) => implementation.send(message));
this.connection.off('Welcome', this.callbackForWelcome);
this.connection.off('Send', this.callbackForSend);
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/HelloSignalR/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,29 @@ namespace HelloSignalR
{
public class Startup
{
readonly string AllowAllPolicy = "AllowAllPolicy";

public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(AllowAllPolicy,
builder =>
{
builder
.AllowAnyMethod()
.AllowAnyHeader()
.AllowAnyOrigin()
.AllowCredentials();
});
});
services.AddSignalR();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();
app.UseCors(AllowAllPolicy);
app.UseSignalR(routes =>
{
routes.MapHub<ChatHub>("/chat");
Expand Down
2 changes: 2 additions & 0 deletions src/HelloSignalR/start_server.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SET ASPNETCORE_URLS=http://localhost:61327
dotnet run --no-launch-profile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HubConnection, IStreamResult } from "@aspnet/signalr"
import { HubConnection, IStreamResult } from '@aspnet/signalr'
{% for hub in Hubs -%}

{{ hub }}
Expand Down
9 changes: 7 additions & 2 deletions src/SigSpec.CodeGeneration.TypeScript/Templates/Hub.liquid
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export class {{ Name }}Hub {
{% for operation in Callbacks -%}
callbackFor{{ operation.Name }}: ({% for parameter in operation.Parameters %}{{ parameter.Name }}: {{ parameter.Type }}{% if forloop.last == false %}, {% endif %}{% endfor %}) => void;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add private?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be safe this should be an array of registrations so if multiple impls are registered all callbacks are unregistered, not only the last one. But we can also leave it as is as this is an edge case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modifications were made following your comments. Please check.

{% endfor -%}

constructor(private connection: HubConnection) {
}
{% for operation in Operations -%}
Expand All @@ -20,13 +24,14 @@

registerCallbacks(implementation: I{{ Name }}HubCallbacks) {
{% for operation in Callbacks -%}
this.connection.on('{{ operation.Name }}', ({% for parameter in operation.Parameters %}{{ parameter.Name }}{% if forloop.last == false %}, {% endif %}{% endfor %}) => implementation.{{operation.MethodName}}({% for parameter in operation.Parameters %}{{ parameter.Name }}{% if forloop.last == false %}, {% endif %}{% endfor %}));
this.callbackFor{{ operation.Name }} = ({% for parameter in operation.Parameters %}{{ parameter.Name }}{% if forloop.last == false %}, {% endif %}{% endfor %}) => implementation.{{operation.MethodName}}({% for parameter in operation.Parameters %}{{ parameter.Name }}{% if forloop.last == false %}, {% endif %}{% endfor %});
this.connection.on('{{ operation.Name }}', this.callbackFor{{ operation.Name }});
{% endfor -%}
}

unregisterCallbacks(implementation: I{{ Name }}HubCallbacks) {
{% for operation in Callbacks -%}
this.connection.off('{{ operation.Name }}', ({% for parameter in operation.Parameters %}{{ parameter.Name }}{% if forloop.last == false %}, {% endif %}{% endfor %}) => implementation.{{operation.MethodName}}({% for parameter in operation.Parameters %}{{ parameter.Name }}{% if forloop.last == false %}, {% endif %}{% endfor %}));
this.connection.off('{{ operation.Name }}', this.callbackFor{{ operation.Name }});
{% endfor -%}
}
}
Expand Down