1
votes

I'm developing a SignalR application in which set of .NET console clients connecting to a web server. How can I call a specific client from the server using SignalR.

In the current application I did, when I click button from the server side. It triggers all the Console clients. But what I want to trigger is keeping all client information on server.html and call specific client.

Currently this is my console application

class Program
{
    static void Main(string[] args)
    {
        var connection = new Connection("http://localhost:65145/printer");

        //Establishing the connection
        connection.Start().ContinueWith(task =>
        {
            if (task.IsFaulted)
            {
                Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
            }
            else
            {
                Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId);
            }
        });

        //Reciveing data from the server
        connection.Received += data =>
        {
            Console.WriteLine("Receiving print request from server");
            Console.WriteLine(data);    
        };

        Console.ReadLine();
    }
}

This is the server side.

server html which calls clients

<script type="text/javascript">
        $(function () {
            var connection = $.connection('/printer');

          connection.received(function (data) {
              //$('#messages').append('<li>' + data + '</li>');
          });

          connection.start().done(function() { 
              $('#print').click(function () {
                  var printThis = { value: 113, reportId: 'Report', printer: '3rd Floor Lazer Printer', Copies: 1 };
                  connection.send(JSON.stringify(printThis));
              });
          });
      });
  </script>

Global.asax

    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.MapHubs();
        RouteTable.Routes.MapConnection<MyConnection>("printer", "/printer");

    }
1
Did you read the documentation github.com/SignalR/SignalR/wiki/… - davidfowl
Yes. But if you can see I'm using connection instead of Hubs. Is going ahead with connection not recommended? - Nipuna

1 Answers