显示SignalR聊天应用程序中当天发生的转换

时间:2015-11-19 22:39:21

标签: asp.net asp.net-mvc signalr chat

我正在做#34; SignalR 2入门和MVC 5" www.asp.net(http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr-and-mvc)中的教程,在其中我使用mvc和signalR开发了一个聊天应用程序 聊天的问题是,一旦你开始聊天,你只能看到从那个时间开始的消息。我希望能够看到那天发生的对话。 我已经在表格中进行了整个聊天对话"聊天":

public class Chat    
    {
        public   int id { get; set; }
        public DateTime date { get; set; }

        public String name { get; set; }
        public String message { get; set; }
}

这是聊天视图:

@{
    ViewBag.Title = "Chat";
}
@{
    Layout = "~/Views/Shared/_LayoutAdherent.cshtml";
}

<h2>Chat</h2>
<div class="container">
    <input type="text" id="message" />
    <input type="button" id="sendmessage" value="Send" />
    <input type="hidden" value=@ViewBag.Name 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="~/Scripts/jquery.signalR-2.2.0.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;
            // 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>');
            };


            // 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($('#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>
}

控制器:

public ActionResult Chat()
        {
            Member member = (Member)Session["logedmem"];
            String name = member.name; 
            ViewBag.Name = name;
            return View();
        }

0 个答案:

没有答案