C# Net客户端:如何向组发送消息?

C# Net客户端:如何向组发送消息?,c#,asp.net,signalr,C#,Asp.net,Signalr,我正在使用SignalR Wiki入门页面中的示例聊天应用程序。我已经扩展了它以添加组支持,它工作得很好 但是,现在我想从外部控制台应用程序向组发送一条消息。这是我的控制台应用程序代码,下面是我的组代码。如何从代理向组发送消息?可能吗 // Console App using System; using Microsoft.AspNet.SignalR.Client.Hubs; namespace SignalrNetClient { class Program {

我正在使用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 Web应用程序主机:

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>

$(函数(){
//动态创建的代理
var chat=$.connection.chat;
//在聊天中心上声明一个函数,以便服务器可以调用它
chat.client.addMessage=函数(消息){
$(“#消息”).append(“
  • ”+消息+”
  • ”); }; $.connection.hub.start(函数(){ chat.server.join(“RoomA”); }); //启动连接 $.connection.hub.start().done(函数(){ $(“#广播”)。单击(函数(){ //在服务器上调用chat方法 chat.server.send($('#msg').val()); }); }); });

    我用类似的方法创建了一个方法,该方法接受您选择的对象,例如

    你的新班级

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

    我想您在发送消息之前知道组名吗?是的,我有组名。现在我将组名与消息组合,然后在Send方法中拆分字符串。然而,我很好奇是否有一个更优雅的解决方案。为什么您需要事先输入组名?发送方法不能扩展为包含组名,然后客户端决定从其机器发送到哪个组吗?非常好的解决方案。谢谢没问题。我认为代码是正确的。这是在我脑子里想出来的,所以试试吧。我想打字的代码有问题。MyMessage类属性名GroupName,但您在您的代码Clients.Group(message.Group.addMessage)中使用了它(“Group message”+message.Msg);应该是message.GroupName?“我说得对吗?”托马斯:是的,那是打字机。现在已经编辑好了。
    chat.server.send({ Msg: "Hello World", Group: "RoomA" });