I have an asp.net classic website. ive got SignalR basic functionality to work (where one client send messages to rest of the clients). but now i want to send Messages only to specific connectionsIDs. I've found some solution via stackoverflow but It didn't work on my codes. I have shared my c# code at below can you guys take a look at it please. my PROBLEM iS that I am able to trigger the hub method "Static_Send" but no way to trigger to the js trigger no response ! any solution please ...
Here is my hub class below;
public class ChatHub : Hub
{
private static IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
public static string ConnectionID;
public void Send( string message, string connectionid)
{
Clients.Client(connectionid).addNewMessageToPage(message, connectionid);
}
public void SendProxy(string name, string message, string connectionid, IHubContext hubContext)
{
hubContext.Clients.All.addNewMessageToPage(name, message, connectionid);
}
public override Task OnConnected()
{
ConnectionID = Context.ConnectionId.ToString();
string username = Context.User.Identity.Name;
return base.OnConnected();
}
public static void Static_Send(string message, string connectionid)
{
//hubContext.Clients.All.addNewMessageToPage(name, message, connectionid);
hubContext.Clients.Client(connectionid).addNewMessageToPage(message, connectionid);
// hubContext.Clients.All.addNewMessageToPage(message, connectionid);
}
}
the below code is content my Mvc controller method
public async System.Threading.Tasks.Task<ActionResult> Tetikle()
{
var hubConnection = new HubConnection("http://localhost:57714");
var hubProxy = hubConnection.CreateHubProxy("ChatHub");
await hubConnection.Start();
var connectionID = hubConnection.ConnectionId;
}
JS codes
$(function () {
var parent = window.parent.document.body;
var chat = $.connection.chatHub;
// $.connection.hub.start();
chat.client.addNewMessageToPage = function (message, connectionid) {
debugger;
console.info("11"+connectionid);
$('.dropdown-menu .listview .lv-body', parent).prepend('<a class="lv-item" href="#">' +
'<div class="media">' +
'<div class="pull-left">' +
'<img class="lv-img-sm" src="img/profile-pics/1.jpg" alt="">' +
'</div>' +
' <div class="media-body">' +
'<div class="lv-title">E-imza</div>' +
'<small class="lv-small">' + message + '</small>' +
'</div>' +
'</div>' +
'</a>');
};
$.connection.hub.start().done(function () {
debugger;
});
});
function htmlEncode(value) {
var encodedValue = $('<div />').text(value).html();
return encodedValue;
}
//});
Where static method has been called in another MVC controller
[HttpPost]
public ActionResult BasvuruCreate(Ruhsat model)
{
ChatHub.Static_Send("aa", item.identity);
}