Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The following steps describe how to connect azure SignalR service in ASP.NET core. See Azure SignalR samples for more samples.
- First, you are going to need to use SignalR in ConfigureServices. It looks like the following:
- /**
- * This sample is for ASP.NET core.
- */
- public class Startup
- {
- public Startup(IConfiguration configuration)
- {
- Configuration = configuration;
- }
- public IConfiguration Configuration { get; }
- // This method gets called by the runtime. Use this method to add services to the container.
- // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddMvc();
- services.AddSignalR()
- .AddAzureSignalR();
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- app.UseMvc();
- app.UseFileServer();
- app.UseAzureSignalR(routes =>
- {
- routes.MapHub("/chat");
- });
- }
- }
- And your hub looks like the following:
- public class Chat : Hub
- {
- public void BroadcastMessage(string name, string message)
- {
- Clients.All.SendAsync("broadcastMessage", name, message);
- }
- public void Echo(string name, string message)
- {
- Clients.Client(Context.ConnectionId).SendAsync("echo", name, message + " (echo from server)");
- }
- }
- In your client scripts, using the following logic to get connected to the service:
- function bindConnectionMessage(connection) {
- var messageCallback = function (name, message) {
- if (!message) return;
- // deal with the message
- alert("message received:" + message);
- };
- // Create a function that the hub can call to broadcast messages.
- connection.on('broadcastMessage', messageCallback);
- connection.on('echo', messageCallback);
- }
- var connection = new signalR.HubConnectionBuilder()
- .withUrl('/chat')
- .build();
- bindConnectionMessage(connection);
- connection.start()
- .then(function () {
- onConnected(connection);
- })
- .catch(function (error) {
- console.error(error.message);
- });
Advertisement
Add Comment
Please, Sign In to add comment