【问题标题】:SignalR Live Chat (automatically get username)SignalR Live Chat(自动获取用户名)
【发布时间】:2014-09-17 22:49:46
【问题描述】:

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

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

如您所见,我正在尝试通过 ChatHub.cs 获取名称,但我不知道如何使用 SignalR JavaScript 使其全部工作。

提前谢谢你。

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

ChatHub.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);
        }
    }
}

Startup.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();
        }
    }
}

【问题讨论】:

    标签: asp.net signalr razor-3


    【解决方案1】:

    如果您使用的是 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");
    

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

    【讨论】:

    • Ooohhhhh 我不知道为什么我没想到!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 2023-04-03
    • 2020-05-28
    • 2019-01-17
    • 2020-11-21
    • 1970-01-01
    • 2014-05-08
    相关资源
    最近更新 更多