SignalR .Net客户端:我如何将消息发送给组? [英] SignalR .Net client: How do I send a message to a Group?

查看:150
本文介绍了SignalR .Net客户端:我如何将消息发送给组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用从SignalR维基样本聊天应用程序入门集线器页。我已经扩展它来添加组的支持,这是工作的罚款。

不过,现在我想从外部的控制台应用程序发送消息到组。这里是我的code的控制台应用程序,并低于我的code代表组。如何从一个代理发送消息给组?这可能吗?

  //控制台应用程序
使用系统;
使用Microsoft.AspNet.SignalR.Client.Hubs;命名空间SignalrNetClient
{
    类节目
    {
        静态无效的主要(字串[] args)
        {
            //连接到服务
            VAR连接=新HubConnection(HTTP://本地主机:50116);
            VAR chatHub = connection.CreateHubProxy(聊天);            //打印消息,当谈到在
            connection.Received + =数据=> Console.WriteLine(数据);            //开始连接
            connection.Start()等待()。            chatHub.Invoke(发送,嘿!);            串线= NULL;
            而((行=到Console.ReadLine())!= NULL)
            {
                //将消息发送到服务器
                connection.Send(线).Wait();
            }
        }
    }
}

SignalR Web App的主机:

 命名空间SignalrServer.Hubs
{
    公共类聊天:集线器
    {
        公共无效发送(字符串消息)
        {
            //调用所有客户端的方法addMessage方法
            Clients.All.addMessage(消息);
            Clients.Group(RoomA)方法addMessage(「本集团消息+消息)。
        }        //服务器
        公共无效加入(串组名)
        {
            Groups.Add(Context.ConnectionId,组名);
        }
    }
}

Default.aspx的

 <脚本SRC =HTTP://$c$c.jquery.com/jquery-1.8.2.min.js类型=文/ JavaScript的> < / SCRIPT>
<脚本SRC =脚本/ jquery.signalR-1.0.1.min.js类型=文/ JavaScript的>< / SCRIPT>
<! - 如果这是一个MVC项目,然后使用下面的 - >
<! - <脚本SRC =〜/ signalr /集线器TYPE =文/ JavaScript的>< / SCRIPT> - >
<脚本SRC =signalr /集线器TYPE =文/ JavaScript的>< / SCRIPT>
<脚本类型=文/ JavaScript的>
    $(函数(){
        //代理动态创建
        VAR聊天= $ .connection.chat;        //声明聊天枢纽功能使服务器可以调用它
        chat.client.addMessage =功能(消息){
            $('#消息)追加('<立GT;'+信息+'< /李>');
        };        $ .connection.hub.start(函数(){
            chat.server.join(RoomA);
        });        //开始连接
        $ .connection.hub.start()。完成(功能(){            $(#广播)。点击(函数(){
                //调用服务器上的聊天方式
                chat.server.send($('#味精)VAL());
            });
        });
    });
< / SCRIPT>  < D​​IV>
    <输入类型=文本ID =味精/>
<输入类型=按钮ID =广播值=广播/>< UL ID =消息>
< / UL>
  < / DIV>


解决方案

我已经用类似的东西是创建一个接受你的选择,例如对象的一个​​方法。完成

您的新类

 公共类MyMessage {
    公共字符串消息{搞定;组; }
    公共字符串组名{获得;组; }
}

然后创建在接受该对象集线器的方法。

 公共无效发送(MyMessage消息)
{
    //调用所有客户端的方法addMessage方法
    Clients.All.addMessage(message.Msg);
    Clients.Group(message.GroupName).addMessage(「本集团消息+ message.Msg);
}

然后从你的客户,你就可以通过这个对象

  chatHub.Invoke< MyMessage>(发送,新MyMessage(){味精=Hello World的,集团=RoomA});

您可以随后也从JS客户端调用此

  chat.server.send({消息:Hello World的,集团:RoomA});

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>

解决方案

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

这篇关于SignalR .Net客户端:我如何将消息发送给组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆