Handles the connection and communication with a SignalR Hub.
May wrap many <pacem-hub-listener>
elements, these handle specific message types pushed by the hub.
// server-side C# code (SignalR Hub)
public class DummyHub : Hub
{
[HubMethodName("methodJ")]
public async Task Send(params string[] args)
{
string sender = Context.User.Identity.Name;
await Clients.All.SendAsync("event1", sender, args[0]);
// ...
await Clients.All.SendAsync("eventN", sender, args[args.Length-1]);
}
}
<!-- HTML markup sample (receiving) -->
<pacem-hub-proxy id="hub" url="/hub-endpoint">
<pacem-hub-listener method="event1" on-receive="<do-something>"></pacem-hub-listener>
...
<pacem-hub-listener method="eventN" on-receive="<do-something-else>"></pacem-hub-listener>
</pacem-hub-proxy>
<!-- HTML markup sample (sending) -->
<pacem-button on-click="#hub.send('methodJ', 'arg1', 'arg2', ..., 'argN')"></pacem-button>
PacemHubListenerElement
s also support streams:
// server-side C# code (SignalR Hub)
[HubMethodName("timer")]
public async IAsyncEnumerable<int> TimerStreamAsync(DateTimeOffset start, [EnumeratorCancellation] CancellationToken token)
{
int timeout = 30;
while (true)
{
int elapsed = (int)DateTimeOffset.UtcNow.Subtract(start).TotalSeconds;
if (elapsed >= timeout)
{
yield return timeout;
break;
}
yield return elapsed;
await Task.Delay(1000, token);
}
}
<!-- HTML markup sample (listening to stream method) -->
<pacem-hub-listener method="timer" stream="true" arguments="{{ [Pacem.Utils.Dates.now().toISOString()] }}" on-receive="console.log($event.detail[0])" on-error="console.log($event.detail)" on-complete="console.log('stream completed')"></pacem-hub-listener>
Here's a working DEMO: any user gets anonymously logged while joining, leaving or posting something, while you only get the exact feedback of what you posted.