【问题标题】:SignalR .Net client: How do I send a message to a Group?SignalR .Net 客户端:如何向组发送消息?
【发布时间】:2013-04-16 13:44:16
【问题描述】:

我正在使用 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);
        }
    }
}

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

【问题讨论】:

  • 我认为您在发送消息之前知道该组的名称?
  • 是的,我有组名。
  • 现在我将组名与消息结合起来,然后在 Send 方法中拆分字符串。但是,我很好奇是否有更优雅的解决方案。
  • 为什么需要组名?不能将 Send 方法扩展为包含组名,然后客户端决定从他们的机器发送到哪个组?

标签: c# asp.net signalr


【解决方案1】:

我所做的类似事情是创建一个接受您选择的对象的方法,例如

你的新班级

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

然后在 Hub 中创建一个接受该对象的方法。

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

【讨论】:

  • 非常好的解决方案。谢谢!
  • 没有问题。我认为代码是正确的。它是在我的头上完成的,所以试一试。
  • 我猜代码中有错字的问题。 MyMessage 类属性名称 GroupName 但你在你的代码中使用 Clients.Group(message.Group).addMessage("Group Message" + message.Msg);它应该是 message.GroupName ?我说的对吗?
  • @Thomas 是的,那是打字机。现已编辑。
猜你喜欢
  • 1970-01-01
  • 2016-09-19
  • 1970-01-01
  • 2022-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-12
  • 1970-01-01
相关资源
最近更新 更多