0

我一直在关注 SignalR 实时聊天教程(http://www.asp.net/signalr/overview/signalr-20/getting-started-with-signalr-20/tutorial-getting-started-with-signalr-20 ),效果很好!尽管如此,为了尝试自己学习一点,我一直在尝试对其进行配置以符合我的需要。

我正在尝试删除用户名提示并使其自动获取登录用户的用户名。只有登录的用户才能访问聊天页面,所以。

如您所见,我正在尝试通过 ChatHub.cs 获取名称,但我无法弄清楚如何使用 SignalR JavaScript 使所有这些都工作。

先感谢您。

聊天.cshtml:

@{
    Page.Title = "Chat";
}

<div class="content">
    <h2>@Page.Title</h2>

    <style type="text/css">
        .container {
            background-color: #99CCFF;
            border: thick solid #808080;
            padding: 20px;
            margin: 20px;
        }
    </style>

    <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>

    <script src="~/assets/js/jquery-2.1.1.min.js"></script>
    <script src="~/Scripts/jquery.signalR-2.1.2.min.js"></script>
    <script src="~/signalr/hubs"></script>
    <script type="text/javascript">
        $(function () {
            // Declare a proxy to reference the hub.
            var chat = $.connection.chatHub;
            // Create a function that the hub can call to broadcast messages.
            chat.client.broadcastMessage = function (name, message) {
                // Html encode display name and message.
                var encodedName = $('<div />').text(name).html();
                var encodedMsg = $('<div />').text(message).html();
                // Add the message to the page.
                $('#discussion').append('<li><strong>' + encodedName
                    + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</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().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();
                });
            });
        });
    </script>


</div>

聊天中心.cs

using System;
using System.Web;
using Microsoft.AspNet.SignalR;
using WebMatrix.Data;
using WebMatrix.WebData;

namespace SignalRChat
{
    public class ChatHub : Hub
    {
        public void Send(string message)
        {
            // Call the broadcastMessage method to update clients.
            var db = Database.Open("GamersInvest");
            string name = db.QueryValue("SELECT UserName FROM Users WHERE UserId = @0", WebSecurity.CurrentUserId);
            Clients.All.broadcastMessage(name, message);
        }
    }
}

启动.cs

using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(SignalRChat.Startup))]
namespace SignalRChat
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here
            app.MapSignalR();
        }
    }
}
4

1 回答 1

0

如果您使用的是 ASP.NET Identity,则可以User.Identity.GetUserName()在视图中使用来获取当前登录用户的名称。如果您使用表单身份验证,请使用User.Identity.Name用户名。您可以使用 Razor 将它们拉入您的 JavaScript:

例如,替换

// Get the user name and store it to prepend to messages.    
$('#displayname').val(prompt('Enter your name:', ''));

有类似的东西

// Get the user name and store it to prepend to messages.
$('#displayname').val("@HttpContext.Current.User.Identity.Name");

请务必为您使用的任何身份系统输入正确的代码。

于 2014-09-17T14:49:46.457 回答