22

I am using the sample Chat application from the SignalR Wiki Getting Started Hubs page. I have extended it to add Group support and it is working fine.

However, now I want to send a message to the group from an external Console application. Here is my code for the Console app and below that my code for Groups. How do I send a message to a Group from a proxy? Is it possible?

// Console App
using System;
using Microsoft.AspNet.SignalR.Client.Hubs;

namespace SignalrNetClient
{
    class Program
    {
        static void Main(string[] args)
        {
            // Connect to the service
            var connection = new HubConnection("http://localhost:50116");
            var chatHub = connection.CreateHubProxy("Chat");

            // Print the message when it comes in
            connection.Received += data => Console.WriteLine(data);

            // Start the connection
            connection.Start().Wait();

            chatHub.Invoke("Send", "Hey there!");

            string line = null;
            while ((line = Console.ReadLine()) != null)
            {
                // Send a message to the server
                connection.Send(line).Wait();
            }
        }
    }
}

SignalR Web App Host:

namespace SignalrServer.Hubs
{
    public class Chat : Hub
    {
        public void Send(string message)
        {
            // Call the addMessage method on all clients            
            Clients.All.addMessage(message);
            Clients.Group("RoomA").addMessage("Group Message " + message);
        }

        //server
        public void Join(string groupName)
        {
            Groups.Add(Context.ConnectionId, groupName);
        }
    }
}

Default.aspx

<script src="http://code.jquery.com/jquery-1.8.2.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.signalR-1.0.1.min.js" type="text/javascript"></script>
<!--  If this is an MVC project then use the following -->
<!--  <script src="~/signalr/hubs" type="text/javascript"></script> -->
<script src="signalr/hubs" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        // Proxy created on the fly          
        var chat = $.connection.chat;

        // Declare a function on the chat hub so the server can invoke it          
        chat.client.addMessage = function (message) {
            $('#messages').append('<li>' + message + '</li>');
        };

        $.connection.hub.start(function () {
            chat.server.join("RoomA");
        });

        // Start the connection
        $.connection.hub.start().done(function () {

            $("#broadcast").click(function () {
                // Call the chat method on the server
                chat.server.send($('#msg').val());
            });
        });
    });
</script>

  <div>
    <input type="text" id="msg" />
<input type="button" id="broadcast" value="broadcast" />

<ul id="messages">
</ul>
  </div>
Chris
  • 44,602
  • 16
  • 137
  • 156
robrtc
  • 2,747
  • 1
  • 17
  • 22
  • I take it you know the name of the group prior to sending the message? – Tim B James Apr 23 '13 at 11:55
  • Yes, I have the group name. – robrtc Apr 23 '13 at 12:24
  • For now I combine the group name with the message and then split the string inside the Send method. However, I am curious if there is a more elegant solution to this. – robrtc Apr 23 '13 at 12:59
  • Why do you need the group name prior? Can't the Send method be extended to include the group name and then the client decides what group to send to from their machine? – ScottFoster1000 Jun 03 '18 at 09:20

1 Answers1

29

What I have done with something similar is to create a method which accepts an object of your choice, e.g.

Your new class

public class MyMessage{
    public string Msg { get; set; }
    public string Group { get; set; }
}

Then create a method in the Hub that accepts this object.

public void Send(MyMessage message)
{
    // Call the addMessage method on all clients            
    Clients.All.addMessage(message.Msg);
    Clients.Group(message.Group).addMessage("Group Message " + message.Msg);
}

Then from your client, you can then pass this object in.

chatHub.Invoke<MyMessage>("send", new MyMessage() { Msg = "Hello World", Group = "RoomA" });

You can then also call this from the JS client

chat.server.send({ Msg: "Hello World", Group: "RoomA" });
Tim B James
  • 20,084
  • 4
  • 73
  • 103
  • No probs. I think the code is correct. It was done off the top of my head, so give it a go. – Tim B James Apr 23 '13 at 13:30
  • i guess there is problem in code for typo. MyMessage class property name GroupName but u used in ur code Clients.Group(message.Group).addMessage("Group Message " + message.Msg); it should be message.GroupName ? am i right? – Thomas Jan 01 '15 at 18:06
  • @Thomas yeah that is a typeo. It has been edited now. – Tim B James Jan 09 '15 at 16:47