only anonymous need an user name

This commit is contained in:
2016-11-01 23:21:03 +01:00
parent 78d91658fb
commit b75c5c7839
2 changed files with 61 additions and 8 deletions

View File

@ -51,10 +51,11 @@ namespace Yavsc.Controllers
public ActionResult Chat() public ActionResult Chat()
{ {
if (User.Identity.IsAuthenticated) { if (User.Identity.IsAuthenticated) {
ViewBag.IsAuthenticated=true;
string uid = User.GetUserId(); string uid = User.GetUserId();
ViewBag.Contacts = DbContext.Contacts.Where(c=>c.OwnerId == uid) ViewBag.Contacts = DbContext.Contacts.Where(c=>c.OwnerId == uid)
; ;
} } else ViewBag.IsAuthenticated=false;
return View(); return View();
} }

View File

@ -9,9 +9,8 @@
font-family: monospace; font-family: monospace;
} }
.notif { .notif {
color: black; color: grey;
font-family: monospace; font-family: monospace;
font-style: italic;
} }
.pv { .pv {
color: black; color: black;
@ -33,9 +32,14 @@
} }
</select> </select>
} }
<ul id="userlist">
</ul>
<ul id="discussion"> <ul id="discussion">
</ul> </ul>
</div> </div>
<environment names="Development">
<button id="GetUsersBtn">Users</button>
</environment>
@section scripts { @section scripts {
<!--Script references. --> <!--Script references. -->
<!--The jQuery library is required and is referenced by default in _Layout.cshtml. --> <!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
@ -46,6 +50,10 @@
<!--SignalR script to update the chat page and send messages.--> <!--SignalR script to update the chat page and send messages.-->
<script> <script>
$(function () { $(function () {
// Reference the auto-generated proxy for the hub. // Reference the auto-generated proxy for the hub.
var chat = $.connection.chatHub; var chat = $.connection.chatHub;
// Create a function that the hub can call back to display messages. // Create a function that the hub can call back to display messages.
@ -59,13 +67,40 @@
$('#discussion').append('<li class="pv"><strong>' + htmlEncode(name) $('#discussion').append('<li class="pv"><strong>' + htmlEncode(name)
+ '</strong>: ' + htmlEncode(message) + '</li>'); + '</strong>: ' + htmlEncode(message) + '</li>');
}; };
chat.client.notify = function (tag, message, data) { var onUserConnected = function (cxid, username) {
// Add the pv to the page. $('#userlist').append('<li class="user">'+username+'</li>')
$('#discussion').append('<li class="notif"><i>' + htmlEncode(tag)
+ '</i> ' + htmlEncode(message) +' '+ htmlEncode(data) +'</li>');
}; };
var onUserDisconnected = function (cxid, username) {
$('#userlist li[data-uid='+cxid+']').remove();
};
chat.client.notify = function (tag, message, data) {
if (data) {
// Add the pv to the page.
if (tag === 'connected') {
onUserConnected(message, data);
$('#discussion').append('<li class="notif"><i>' + htmlEncode(tag)
+ '</i> ' + htmlEncode(data) +'</li>');
}
else if (tag === 'disconnected')
{ 
onUserDisconnected(message, data);
$('#discussion').append('<li class="notif"><i>' + htmlEncode(tag)
+ '</i> ' + htmlEncode(data) +'</li>');
}
else {
$('#discussion').append('<li class="notif"><i>' + htmlEncode(tag)
+ '</i> ' + htmlEncode(message) + ' : ' + htmlEncode(data) + '</li>');
}
}
};
@if (!ViewBag.IsAuthenticated) {
// Get the user name and store it to prepend to messages. // Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', '')); <text>
$('#displayname').val(prompt('Enter your name:', ''));
</text>
}
// Set initial focus to message input box. // Set initial focus to message input box.
$('#message').focus(); $('#message').focus();
// Start the connection. // Start the connection.
@ -82,7 +117,24 @@
// Clear text box and reset focus for next comment. // Clear text box and reset focus for next comment.
$('#message').val('').focus(); $('#message').val('').focus();
}); });
var getUsers = function() {
$('#userlist').empty();
chat.server.getUserList().done(
function(users) {
$.each(users, function () {
var user = this;
$('#userlist').append('<li class="user"><i>' + htmlEncode(user.UserName)
+ '</i> ' + htmlEncode(user.ConnectionId) + '</li>');
});
}
);
console.log(res);
};
$("#GetUsersBtn").click(getUsers);
}); });
}); });
// This optional function html-encodes messages for display in the page. // This optional function html-encodes messages for display in the page.
function htmlEncode(value) { function htmlEncode(value) {