【问题标题】:Identifying MVC view from SignalR ChatHub从 SignalR ChatHub 识别 MVC 视图
【发布时间】:2013-12-13 12:10:00
【问题描述】:

我有一个基于 Asp.net MVCSignalR 的简单聊天应用程序。我打算在每个“聊天室”中拥有一个 view 在这个应用程序中。每个“聊天室”都是一个信号组。我试图弄清楚服务器端如何知道 一个人在特定的view 中,然后将他/她的 connectionId 映射到该组。我有这样做的想法 在OnConnected() 任务中,当用户连接到聊天集时该任务会被击中。它看起来像这样:

public override async Task OnConnected()
        {
            if("view" == "Room1")
            {
            await Groups.Add(Context.ConnectionId, "Room1");
            }
        }

但是我如何获得"view"?也许有人知道这样做的不同方法?

以下是当前大部分视图的外观:

@model PagedList.IPagedList<ChatProj.Models.Message>
@using PagedList.Mvc; 
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
    ViewBag.Title = "Chat";
}

<h2>Chat</h2>

<style>
ul {list-style-type:circle;}
</style>

<div class="container">



    <div class="nano chat">
        <div class="content">

            <ul id="discussion">
            </ul> 
        </div> 
    </div>

    <input type="text" id="message" />

    <input type="button" id="sendmessage" value="Send" disabled="disabled" />

    <input type="hidden" id="displayname" />

</div>

@section scripts {

    <!--Script references. -->
    <!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
    <!--Reference the SignalR library. -->
    <script src="~/Scripts/jquery.signalR-1.1.3.js"></script>
    <script src="~/Scripts/jquery.nanoscroller.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.chatHub;
            $(".nano").nanoScroller();
            // Create a function that the hub can call back to display messages.
            chat.client.addNewMessageToPage = function (name, message) {
                // Add the message to the page. 
                $('#discussion').append('<li><strong>' + htmlEncode(name)
                    + '</strong>: ' + htmlEncode(message) + '</li>');
            };

            $(document).ready(function () {
                $("#sendmessage").removeAttr("disabled");
                $('#message').keypress(function (e) {
                    if (e.keyCode == 13)
                        $('#sendmessage').click();
                });
            });
            // Get the user name and store it to prepend to messages.

            // Set initial focus to message input box.  
            $('#message').focus();
            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                    // Call the Send method on the hub. 
                    chat.server.send($('#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>
    }

【问题讨论】:

  • 为什么不用用户的Session 来代替呢?具有相同Session 值的所有用户将被放置在同一个“房间”中。

标签: asp.net-mvc model-view-controller razor signalr


【解决方案1】:

就我个人而言,我不会为每个聊天室创建单独的页面,而是将其设为 SPA,其中包含加入和离开聊天室的中心方法。这样您就不必为每个房间建立新的连接,并且您可以在多个房间中容纳人员,而不会与浏览器的连接限制(通常每个域 6 个并发连接)发生冲突。

无论如何,按照自己的方式进行操作,您可以使用 query string 将房间名称传递给 OnConnected:

$.connection.hub.qs = { "room": "main" };
....
$.connection.hub.start();

并在 OnConnected 中阅读:

string room = Context.QueryString["room"];
if (room == ...) { ... }

【讨论】:

  • 为回答伙伴干杯。现在让它工作,只需将“main”更改为我想要的那个视图的任何唯一名称。顺便说一句,SPA 代表什么?我正在考虑在应用程序中实施 /join group 命令,以便可以加入/创建任何组。我为每个组创建了特定的页面,以便用户更直观。但不需要。
  • @JazzMaster Single Page Application,即您可以在不重新加载整个页面的情况下切换房间
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-04
相关资源
最近更新 更多