[NoBrainer] Sending SignalR messages from PowerShell to the Server

This is a quick addition to my previous blog post on how to receive SignalR messages via PowerShell. Once you have established a connection in PowerShell to your SignalR hub you can easily them a message to the server via the ‘Invoke()’ method on the IHubProxy object.
In it you specify the name of the method you want to call and its arguments like this:

PS > # $s is a SignalR object
PS > $s.GetType().FullName
SignalRClient.Connection
PS > $t = $s._hub.Invoke("SendMessageToServer", "tralala");
PS > $t
Result                 : 9/5/2014 2:31:16 PM
Id                     : 2
Exception              :
Status                 : RanToCompletion
IsCanceled             : False
IsCompleted            : True
CreationOptions        : None
AsyncState             :
IsFaulted              : False
AsyncWaitHandle        : System.Threading.ManualResetEvent
CompletedSynchronously : False

PS > $t.Result
Friday, September 05, 2014 2:31:16 PM

The SignalR client object can be created and consumed as follows:

In case your browser does not correctly render the Gist code you can view it directly at: https://gist.github.com/dfch/d598378f8a162a9bdf5b

The result of the operation is encapsulated in the “Result” property of the returned task object.

On the server side you just have to define a public method like this:

public string SendMessageToServer(string message)
{
  System.Diagnostics.Debug.WriteLine("{0}@{1}: '{2}'", Context.User.Identity.Name, Context.ConnectionId, message);

  return DateTimeOffset.UtcNow.ToString("s");
}

… that’s all it takes.

Comments

  1. Cool post! Cool for support or bugfixing issues! Here are some internals of SignalR, if you allow me to mention it here: http://robinsedlaczek.wordpress.com/2014/09/11/inside-signalr-addressing-clients-return-values-broken-connections-and-security/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: