【问题标题】:Signal R library for chat is not working for group with mvc asp.net用于聊天的 Signal R 库不适用于具有 mvc asp.net 的组
【发布时间】:2016-02-09 19:46:52
【问题描述】:

我正在使用 SignalR jQuery 库与群组发送聊天,但在我的情况下我看不到可以发送的聊天代码...

我已经为组创建了类并从 jQuery 传递了组,但似乎有些问题,我看不到可以通过聊天窗口发送聊天,任何人都可以帮助我...

类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;

namespace Chat.Hubs
{
    [HubName("myChatHub")]
    public class ChatHub : Hub
    {
        public void Send(MyMessage message)
        {
            // Call the addMessage method on all clients            
            //Clients.All.addNewMessageToPage(message.Msg);
            Clients.Group(message.GroupName).addNewMessageToPage("Group Message " + message.Msg);
        }

        //server
        public void Join(string groupName)
        {
            Groups.Add(Context.ConnectionId, groupName);
        }
    }
    public class MyMessage
    {
        public string Msg { get; set; }
        public string GroupName { get; set; }
    }
}

查看:

@{
    ViewBag.Title = "Chat";
}

<h2>Chat</h2>

<div class="container">
    <input type="text" id="message" />
    <input type="button" id="sendmessage" value="Send" />
    <input type="hidden" id="displayname" />
    <ul id="discussion">
    </ul>
</div>

@section scripts {
    <!--Script references. -->
    <!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
    <!--Reference the SignalR library. -->
    <script src="@Url.Content("~/Scripts/jquery.signalR-1.1.3.min.js")"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="~/signalr/hubs"></script>
    <!--SignalR script to update the chat page and send messages.--> 
    <script>
        $(function () {
            // Reference the auto-generated proxy for the hub.  
            var chat = $.connection.myChatHub;



            // Create a function that the hub can call back to display messages.
            chat.client.addNewMessageToPage = function (message) {
                // Add the message to the page. 
                $('#discussion').append('<li><strong>' + htmlEncode(message) + '</li>');
            };
            // Get the user name and store it to prepend to messages.
            $('#displayname').val(prompt('Enter your name:', ''));
            // Set initial focus to message input box.  
            $('#message').focus();
            // Start the connection.
            $.connection.hub.start(function () {
                chat.server.join("RoomA");
            });

            $.connection.hub.start().done(function () {

                $('#sendmessage').click(function () {
                    // Call the Send method on the hub. 
                    chat.server.send({ Msg: $('#message').val(), Group: "RoomA" });
                    //chat.server.send($('#displayname').val(), $('#message').val());
                    // Clear text box and reset focus for next comment. 
                    $('#message').val('').focus();
                });
            });
        });
        // This optional function html-encodes messages for display in the page.
        function htmlEncode(value) {
            var encodedValue = $('<div />').text(value).html();
            return encodedValue;
        }
    </script>
}

【问题讨论】:

    标签: jquery asp.net-mvc-4 asp.net-web-api chat signalr


    【解决方案1】:

    您的 javascript 对象具有 GroupName 而不是组:

    Clients.Group(message.GroupName).addNewMessageToPage("Group Message " + message.Msg);
    

    你正在发送:

    chat.server.send({ Msg: $('#message').val(), Group: "RoomA" });
    

    注意 Group vs GroupName 属性。

    此外,当该客户端在组中时,从 Groups.Add 返回的任务完成。这意味着您可以从方法中返回任务,以便知道客户端何时在该组中。

    【讨论】:

    • 嗨,对..你是...任何完整的聊天应用程序都可以用于紧急实施...例如 facebook 聊天或 gmail 聊天?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多