消息不会发送到组信号器

时间:2015-06-04 19:42:40

标签: javascript asp.net-mvc asp.net-mvc-4 knockout.js signalr

在信号器中遇到一些问题,无法与群组合作,所以当它有这样的形式时它会起作用:

Clients.All.received(new { sender = sender, message = message, isPrivate = false });

在我的项目中,我有房间,所以当我向群组发送消息时,它们不会显示在页面上:

Clients.Group(chatuser.CurrentGroup).received(new { sender = sender, message = message, isPrivate = false });

这里是javascript的一小部分:

chatHub.client.received = function (message) {
        viewModel.messages.push(new Message(message.sender, message.message, message.isPrivate, 'normalMessage'));
    };

请告诉我,如果您需要查看更多代码,我将使用完整的javascript文件更新我的所有chathub代码和模型。

更新2。 ChatUser:

public ChatUser(string group) {
    CurrentGroup = group;
}

public ChatUser()
    : this("Lobby")
{

}

public string nameOfGroup { get; set; }
public int authorID { get; set; }

public List<string> chatGroup { get; set; }
public string CurrentGroup { get; set; }

public string userLogin { get; set; }
public HashSet<string> ConnectionIds { get; set; }

Chathub.cs

private void SendGroupMessage(ChatUser user, string message)
    {
        Clients.Group(user.CurrentGroup).received(user.userLogin, message);
    }

public void Send(string message)
{
    var user = GetUser(Context.User.Identity.Name);   
    SendGroupMessage(user, message);
}
public override Task OnConnected()
{

    string userName = Context.User.Identity.Name;
    string connectionId = Context.ConnectionId;

    var user = chatUsers.GetOrAdd(userName, _ => new ChatUser
    {
        userLogin = userName,
        ConnectionIds = new HashSet<string>()
    });

    lock (user.ConnectionIds)
    {

        user.ConnectionIds.Add(connectionId);

        // Or you might want to only broadcast this info if this
        // is the first connection of the user
        if (user.ConnectionIds.Count == 1)
        {

            Clients.Others.userConnected(userName);
        }
    }

    return base.OnConnected();
}

public Task JoinGroup(string groupName)
{
    var user = GetUser(Context.User.Identity.Name);
    string connectionGrpToString = user.ConnectionIds.ToString();

    //Join new Group
    user.CurrentGroup = groupName;
    //await
    return Groups.Add(connectionGrpToString, user.CurrentGroup);
}

public Task LeaveRoom(string roomName)
{
    var user = GetUser(Context.User.Identity.Name);
    string connectionGrpToString = user.ConnectionIds.ToString();
    var oldGroup = user.CurrentGroup;

    //Tell everyone user is leaving
    var message = string.Format("{0} has left.", user.userLogin);
    SendGroupAlert(user, message);

    //remove user from the group
    //await
    return Groups.Remove(connectionGrpToString, oldGroup); ;
}

重写Chat.js的一小部分:

function Message(from, msg, template) {
    this.from = ko.observable(from);
    this.message = ko.observable(msg);
    this.template = ko.observable(template);
}

    chatHub.client.received = function (sender, message) {
        // Add the message to the page.
        viewModel.messages.push(new Message(sender, message, 'normalMessage'));
    };

查看chat.cshtml:

<div class="col-md-12">
    <div class="form-search">
        <input type="text" id="txtMsg" class="input-xxlarge" disabled="disabled" placeholder="send" />
        <button type="button" id="btnSend" class="btn btn-info" disabled="disabled">Send</button>
    </div>
</div>
<div class="col-md-10">
    <ul id="normalMessage" data-bind="foreach: messages">
        <li>
            <strong><span data-bind="text: from"></span>: </strong>
            <span data-bind="text: message"></span>
        </li>
    </ul>
</div>

更新3。 也许我错过了一些东西,但也显示了房间页面的代码以及它如何重定向我,我也发现当我选择我要加入的房间时,它会转到方法JoinGroup并且参数groupName它正确地显示了我选择的内容,但在我要发送消息之后,在user.CurrentGroup中它显示我默认的大厅..

Rooms.cshtml:

<div id="nav">
    <div id="rooms" class="well well-small">
        <ul class="nav nav-list">
            <li class="nav-header">Rooms</li>
            @foreach (var item in Model)
            {
                <li data-value=@item.Name>@Html.ActionLink("Room: " + item.Name, "Chat", "Home")</li>
            }
        </ul>
    </div>
</div>

以及我如何加入chat.js:

    var roomKey = $(this).data('value');
    chatHub.server.joinGroup(roomKey);

1 个答案:

答案 0 :(得分:0)

我认为你的问题是这部分,问题是逻辑错误

public Task JoinGroup(string groupName)
{
    var user = GetUser(Context.User.Identity.Name);
    string connectionGrpToString = user.ConnectionIds.ToString();

    //Join new Group
    user.CurrentGroup = groupName;
    //await
    return Groups.Add(connectionGrpToString, user.CurrentGroup);
}

这里您的fetech用户信息然后将组名称分配给它,然后将用户添加到该组。

一切正常,但您忘记使用组名更新您的用户列表。

您指定了组名

    //Join new Group
    user.CurrentGroup = groupName;

但是在您的列表中,用户还没有

中的群组名称
    //Join new Group
    user.CurrentGroup = groupName;

当您从GetUser

中提取用户的send方法时
public void Send(string message)
{
    var user = GetUser(Context.User.Identity.Name);   
    SendGroupMessage(user, message);
}  

用户的群组名称未找到,因为您没有更新用户列表中的群组名称