I have build a livechat with help from https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/tutorial-getting-started-with-signalr
Now i have that problem that i cant use sessionStorage to save the username so the user dont have to type a username everytime he/she enter the chat site on the website.
It's my first time working with web application and sessionStorage.
Hope someone can help me :)
Online view: http://chat.kibshede.dk/Chat.aspx
I have a class with this in it: namespace SignalRChat { public class ChatHub : Hub { public void Send(string name, string message) { // Call the broadcastMessage method to update clients. Clients.All.broadcastMessage(name, message); } } }
and a Owin class with:
namespace SignalRChat { public class Startup { public void Configuration(IAppBuilder app) { // Any connection or hub wire up and configuration should go here app.MapSignalR(); } } }
<!--Reference the autogenerated SignalR hub script. -->
<script src="signalr/hubs"></script>
<!--Add script to update the page and send messages.-->
<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>: ' + encodedMsg + '</li>');
};
if (sessionStorage.getItem('UserName') != '#ContentPlaceHolder1_displayname') {
// Get the user name and store it to prepend to messages.
var UserName = prompt('Enter your name:', '').toString();
sessionStorage.setItem('UserName', UserName);
}
// Set initial focus to message input box.
$('#ContentPlaceHolder1_message').focus();
// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send(sessionStorage.getItem('UserName'), $('#ContentPlaceHolder1_message').val());
// Clear text box and reset focus for next comment.
$('#ContentPlaceHolder1_message').val('').focus();
});
});
});
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<asp:Panel ID="Panel_ChatContainer" CssClass="container" runat="server">
<asp:Panel ID="Panel_ChatContainer_Chat" runat="server">
<asp:TextBox ID="message" runat="server"></asp:TextBox>
<input type="button" id="sendmessage" value="Send" />
<asp:Label ID="displayname" runat="server"></asp:Label>
<ul id="discussion"></ul>
</asp:Panel>
</asp:Panel>