Luis Valencia
Luis Valencia

Reputation: 33978

How to send the LOGGED in user in a chat application using SignalR

I am studying the following example of signalR

http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr-and-mvc-4

I want to implement it, and well the code is there, however I dont want the user to type the username in a prompt window, I am going to make the page available for logged in users, therefore I will have the context.User.

I would like to change the prompt part to use the context.User from the server side but I have no idea how.

thanks a lot

 $('#displayname').val(prompt('Enter your name:', '')); 

Upvotes: 2

Views: 858

Answers (3)

Valentina Troche
Valentina Troche

Reputation: 65

  // If the user is Authenticated shows de nick if not sets it to invitado + random number
        if (Request.IsAuthenticated) {
            $('#displayname').val(User.Identity.GetUserName());
        } else {
            $('#displayname').val("invitado" + Math.floor((Math.random() * 100) + 1));
            // $('#displayname').val(prompt('Enter your name:', '')); (Alias Window... what you had)
        }

Upvotes: 1

Prateek Saini
Prateek Saini

Reputation: 23

I've used the following code. However, it doesn't redirect you to another page. But, it makes the login window invisible as the user log in to application and it displays the chat room. Hope this helps.

$(function () {
        setScreen(false);
        var chatHub = $.connection.chatHub;
        registerClientMethods(chatHub);

        $.connection.hub.start().done(function () {
            registerEvents(chatHub)
        });
    });

function setScreen(isLogin) {
            if (!isLogin) {
                $("#divchat").hide();
                $("#divLogin").show();
            }
            else {
                $("#divchat").show();
                $("#divLogin").hide();
            }

        };

Upvotes: 1

Basic
Basic

Reputation: 26766

You could provide the information on page load or retrieve it via AJAX. I'd suggest the former.

Depending on your view engine, something like...

 $('#displayname').val('@(Context.User.Replace("'", "\\'"))'); 

or Better, provide it as a property of your view model so it would be

 $('#displayname').val('@(Model.User.Replace("'", "\\'"))'); 

Upvotes: 3

Related Questions