I have a project in ASP.NET Core 2.1 MVC SignalR. When I started the project I did not select Individual Account options in authorization. I want to send the message using the user's email address stored in a database instead of the connection Id but I can't achieve this. How can I fix this problem?
This is my hub class code:
public class SignalRChat:Hub
{
Context c = new Context();
public async Task setUserEmail(string email)
{
string id = Context.ConnectionId;
c.EmailConnectionIds.Where(a => a.connection_id == id).FirstOrDefault().email = email;
}
public async Task ClientSendMessage(string connectionId,string user, string message)
{
var deger= c.EmailConnectionIds.Where(a => a.connection_id ==
connectionId).FirstOrDefault();
await Clients.Client(deger.connection_id).SendAsync("ReceiveMessage",user, message);
}
public override async Task OnConnectedAsync()
{
EmailConnectionId val = new EmailConnectionId();
val.connection_id = Context.ConnectionId; ;
val.email = "";
c.EmailConnectionIds.Add(val);
c.SaveChanges();
await base.OnConnectedAsync();
}
public override Task OnDisconnectedAsync(Exception exception)
{
var connection = c.EmailConnectionIds.Where(a => a.connection_id ==
Context.ConnectionId).FirstOrDefault();
if (connection != null)
{
c.EmailConnectionIds.Remove(connection);
}
return base.OnDisconnectedAsync(exception);
}
}
This is my code:
"use strict";
$(document).ready(() => {
var connection = new signalR.HubConnectionBuilder().withUrl("/chathub").build();
var email = $("#client").val();
connection.start().then(() => connection.invoke('setUserEmail ', email));
$("#msg-send").click(() => {
let message = $("#txtMessage").val();
$("#txtMessage").val(" ");
var user = $("#sender").val();
connection.invoke("ClientSendMessage", $("#client").val(), user, message)
.catch(error => console.log("Error." + error));
var div = document.createElement("div");
div.textContent = message;
document.getElementById("chat-cont").appendChild(div);
});
connection.on("ReceiveMessage", function (user, message) {
var msg = message.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
var encodedMsg = msg;
var div = document.createElement("div");
div.textContent = encodedMsg;
document.getElementById("chat-cont").appendChild(div);
});
});
HTML code:
<div class="container">
<div id="chat-cont" class="clearfix" style="max-height:450px;overflow:scroll;">
</div>
<div style="margin-top:470px;margin-left:40%;bottom:50px !important;" class="footer navbar-fixed-bottom">
<div class="row">
<h5>Connection ID : <span id="connectionId"></span></h5>
</div>
<div class="row">
<div class="col-md-7"><input type="text" id="sender" value="@ViewBag.message"></div>
</div>
<div class="row">
<div class="col-md-7"><input type="text" placeholder="ReceiverId" id="client"></div>
</div>
<div class="row">
<div class="col-md-7" style="position:relative;"> <input type="text" id="txtMessage" class="form-control" style="width:70%;"></div>
<div class="col-md-5" style="position:absolute;margin-left:40%;"> <button id="msg-send" class="btn btn-success">Send</button></div>
</div>
</div>
</div>