diff --git a/README.md b/README.md index fecac58..9ae468f 100644 --- a/README.md +++ b/README.md @@ -173,10 +173,15 @@ Generated spec: Generated TypeScript code: ```typescript -import { HubConnection, IStreamResult } from "@aspnet/signalr" +import { HubConnection, IStreamResult } from '@aspnet/signalr'; export class ChatHub { + private callbackForWelcome: () => void; + private callbackForSend: (message: string) => void; + constructor(private connection: HubConnection) { + this.callbackForWelcome = undefined; + this.callbackForSend = undefined; } send(message: string): Promise { @@ -191,14 +196,24 @@ export class ChatHub { return this.connection.stream('GetEvents'); } - registerCallbacks(implementation: IChatHubCallbacks) { - this.connection.on('Welcome', () => implementation.welcome()); - this.connection.on('Send', (message) => implementation.send(message)); + registerCallbacks(implementation: IChatHubCallbacks): void { + this.unregisterCallbacks(); + + 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)); + unregisterCallbacks(): void { + if (this.callbackForWelcome !== undefined) { + this.connection.off('Welcome', this.callbackForWelcome); + this.callbackForWelcome = undefined; + } + if (this.callbackForSend !== undefined) { + this.connection.off('Send', this.callbackForSend); + this.callbackForSend = undefined; + } } } diff --git a/src/HelloSignalR/Startup.cs b/src/HelloSignalR/Startup.cs index f2a238e..b51e118 100644 --- a/src/HelloSignalR/Startup.cs +++ b/src/HelloSignalR/Startup.cs @@ -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("/chat"); diff --git a/src/HelloSignalR/start_server.cmd b/src/HelloSignalR/start_server.cmd new file mode 100644 index 0000000..84475b4 --- /dev/null +++ b/src/HelloSignalR/start_server.cmd @@ -0,0 +1,2 @@ +SET ASPNETCORE_URLS=http://localhost:61327 +dotnet run --no-launch-profile \ No newline at end of file diff --git a/src/SigSpec.CodeGeneration.TypeScript/Templates/File.liquid b/src/SigSpec.CodeGeneration.TypeScript/Templates/File.liquid index de52ac1..96ca016 100644 --- a/src/SigSpec.CodeGeneration.TypeScript/Templates/File.liquid +++ b/src/SigSpec.CodeGeneration.TypeScript/Templates/File.liquid @@ -1,4 +1,4 @@ -import { HubConnection, IStreamResult } from "@microsoft/signalr" +import { HubConnection, IStreamResult } from '@microsoft/signalr'; {% for hub in Hubs -%} {{ hub }} diff --git a/src/SigSpec.CodeGeneration.TypeScript/Templates/Hub.liquid b/src/SigSpec.CodeGeneration.TypeScript/Templates/Hub.liquid index c5f8c74..6de943a 100644 --- a/src/SigSpec.CodeGeneration.TypeScript/Templates/Hub.liquid +++ b/src/SigSpec.CodeGeneration.TypeScript/Templates/Hub.liquid @@ -1,5 +1,12 @@ export class {{ Name }}Hub { +{% for operation in Callbacks -%} + private callbackFor{{ operation.Name }}: ({% for parameter in operation.Parameters %}{{ parameter.Name }}: {{ parameter.Type }}{% if forloop.last == false %}, {% endif %}{% endfor %}) => void; +{% endfor -%} + constructor(private connection: HubConnection) { +{% for operation in Callbacks -%} + this.callbackFor{{ operation.Name }} = undefined; +{% endfor -%} } {% for operation in Operations -%} @@ -18,15 +25,21 @@ {% endif -%} {% endfor -%} - registerCallbacks(implementation: I{{ Name }}HubCallbacks) { + registerCallbacks(implementation: I{{ Name }}HubCallbacks): void { + this.unregisterCallbacks(); + {% 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) { + unregisterCallbacks(): void { {% 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 %})); + if (this.callbackFor{{ operation.Name }} !== undefined) { + this.connection.off('{{ operation.Name }}', this.callbackFor{{ operation.Name }}); + this.callbackFor{{ operation.Name }} = undefined; + } {% endfor -%} } }