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
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:

$eventName = "receiveMessage";
Add-Type Path ".\SignalRClient.dll"
$s = New-Object SignalRClient.Connection("http://localhost/", "commandhub");
while($true) {
Start-Sleep seconds 1
# string TryDequeue(string eventName)
# string Dequeue(string eventName)
# string Dequeue(string eventName, int dwMillisecondsTotalWaitTime)
# string Dequeue(string eventName, int dwMillisecondsTotalWaitTime, int dwMilliSecondsWaitIntervall)
# System.Collections.Generic.List[string] DequeueAll(string eventName)

In case your browser does not correctly render the Gist code you can view it directly at:

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.


Leave a Reply to Ronald Rink Cancel reply

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

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.