Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Send server message to connected clients with Signalr/PersistentConnection
- public class MyConnection : PersistentConnection {
- protected override Task OnReceivedAsync(string clientId, string data) {
- // Broadcast data to all clients
- return Connection.Broadcast(data);
- }
- }
- using System;
- using System.Web.Routing;
- using SignalR.Routing;
- public class Global : System.Web.HttpApplication {
- protected void Application_Start(object sender, EventArgs e) {
- // Register the route for chat
- RouteTable.Routes.MapConnection<MyConnection>("echo", "echo/{*operation}");
- }
- }
- $(function () {
- var connection = $.connection('echo');
- connection.received(function (data) {
- $('#messages').append('<li>' + data + '</li>');
- });
- connection.start();
- $("#broadcast").click(function () {
- connection.send($('#msg').val());
- });
- });
- public bool Join(string channel)
- {
- AddToGroup(channel.ToString()).Wait();
- return true;
- }
- public void Send(string channel, string message)
- {
- String id = this.Context.ClientId;
- Clients[channel.ToString()].addMessage(message);
- }
- using SignalR;
- using SignalR.Hosting.AspNet;
- using SignalR.Infrastructure;
- public class MyConnection : PersistentConnection
- {
- }
- public class Notifier
- {
- public void Notify(string clientId, object data) {
- MyConnection connection = (MyConnection) AspNetHost.DependencyResolver
- .Resolve<IConnectionManager()
- .GetConnection<MyConnection>();
- connection.Send(clientId, data);
- }
- }
Add Comment
Please, Sign In to add comment