SignalR .Net客户端:如何向群组发送消息?

22

我正在使用SignalR Wiki入门教程页面上的示例聊天应用程序。我已经扩展了它以添加组支持,并且它工作正常。

但是,现在我想从外部控制台应用程序向组发送消息。这是我的控制台应用程序代码以及下面的组代码。如何从代理向组发送消息?这是否可能?

// 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网络应用程序主机:

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);
        }
    }
}

默认.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>

你发送消息前已经知道群组的名称了吗? - Tim B James
是的,我有群组名称。 - robrtc
目前我是将群组名称与消息组合,然后在Send方法内部拆分字符串。不过,我很好奇是否有更优雅的解决方案。 - robrtc
为什么需要先指定组名?不能将Send方法扩展以包括组名,然后客户端从其计算机上决定要发送到哪个组吗? - ScottFoster1000
1个回答

29
我曾用类似的方法创建了一个接受您选择的对象的方法,例如:
您的新类
public class MyMessage{
    public string Msg { get; set; }
    public string Group { get; set; }
}

然后在中央控制器中创建一个接受这个对象的方法。

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);
}

然后你可以从你的客户端传递这个对象。

chatHub.Invoke<MyMessage>("send", new MyMessage() { Msg = "Hello World", Group = "RoomA" });
你可以从JS客户端调用这个函数。
chat.server.send({ Msg: "Hello World", Group: "RoomA" });

没问题。我认为代码是正确的。这是我凭记忆完成的,所以试一下吧。 - Tim B James
我猜代码中有一个错别字的问题。MyMessage类的属性名是GroupName,但你在代码中使用了Clients.Group(message.Group).addMessage("Group Message " + message.Msg)。这应该是message.GroupName吧?我说得对吗? - Thomas
@Thomas 是的,那是一个打字错误。现在已经被编辑过了。 - Tim B James

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接