mirror of
https://github.com/chsakell/aspnet-core-signalr-angular.git
synced 2025-12-22 17:27:48 +00:00
integrate Web API and SignalR
This commit is contained in:
21
Controllers/ApiHubController.cs
Normal file
21
Controllers/ApiHubController.cs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.SignalR;
|
||||||
|
using Microsoft.AspNetCore.SignalR.Infrastructure;
|
||||||
|
using Microsoft.AspNetCore.SignalR.Hubs;
|
||||||
|
|
||||||
|
namespace LiveGameFeed.Controllers
|
||||||
|
{
|
||||||
|
public abstract class ApiHubController<T> : Controller
|
||||||
|
where T : Hub
|
||||||
|
{
|
||||||
|
private readonly IHubContext _hub;
|
||||||
|
public IHubConnectionContext<dynamic> Clients { get; private set; }
|
||||||
|
public IGroupManager Groups { get; private set; }
|
||||||
|
protected ApiHubController(IConnectionManager signalRConnectionManager)
|
||||||
|
{
|
||||||
|
var _hub = signalRConnectionManager.GetHubContext<T>();
|
||||||
|
Clients = _hub.Clients;
|
||||||
|
Groups = _hub.Groups;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,12 +3,17 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.SignalR.Infrastructure;
|
||||||
|
using LiveGameFeed.Hubs;
|
||||||
|
|
||||||
namespace LiveGameFeed.Controllers
|
namespace LiveGameFeed.Controllers
|
||||||
{
|
{
|
||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
public class ValuesController : Controller
|
public class ValuesController : ApiHubController<Broadcaster>
|
||||||
{
|
{
|
||||||
|
public ValuesController(IConnectionManager signalRConnectionManager)
|
||||||
|
: base(signalRConnectionManager) { }
|
||||||
|
|
||||||
// GET api/values
|
// GET api/values
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public IEnumerable<string> Get()
|
public IEnumerable<string> Get()
|
||||||
|
|||||||
17
Hubs/Broadcaster.cs
Normal file
17
Hubs/Broadcaster.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.SignalR;
|
||||||
|
|
||||||
|
namespace LiveGameFeed.Hubs
|
||||||
|
{
|
||||||
|
public class Broadcaster : Hub
|
||||||
|
{
|
||||||
|
public override Task OnConnected()
|
||||||
|
{
|
||||||
|
return Clients.All.Message("New connection " + Context.ConnectionId);
|
||||||
|
}
|
||||||
|
public Task Broadcast(string message)
|
||||||
|
{
|
||||||
|
return Clients.All.Message(Context.ConnectionId + "> " + message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user