For the life of me, I can’t figure out how to send the SignalR message only to the client that hit the "Go" button, and no one else. It all works when I send it to "All", but I can’t figure out how to get the connectionID on the server, and then just send the message to the initiating client. This is what I have working so far. I’d appreciate it if you could tell me how to make this work with just the calling client –
This is my Default.aspx file
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SignalR5._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div><br /><br />
<asp:Button ID="btnGo" runat="server" Text="GO!!!" OnClick="btnGo_Click" OnClientClick="this.disabled=true;" UseSubmitBehavior="false" /><br /><br />
<asp:Label ID="lblLog" class="#log" runat="server" Text="Begin"></asp:Label> <br /> <br />
<asp:Label ID="lblConnectionID" runat="server" Text=""></asp:Label>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<script src="Scripts/jquery-3.4.1.js"></script>
<script src="Scripts/jquery.signalR-2.4.2.js"></script>
<script src="signalr/hubs"></script>
<script type="text/javascript">
$(function() {
var logger = $.connection.logHub;
logger.client.logMessage = function(msg) {
$("#MainContent_lblLog").html(msg);
};
$.connection.hub.start().done(function () {
var cid = $.connection.hub.id;
$("#MainContent_lblConnectionID").text("ConnectionID: " + cid);
//alert("connection Id:" + cid);
});
});
</script>
</asp:Content>
This is my Default.aspx.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Diagnostics;
namespace SignalR5
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnGo_Click(object sender, EventArgs e)
{
//The timer is just to display how long each task takes.
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
string strMessage = "Hello ";
LogHub.SendMessage(strMessage);
Thread.Sleep(2000);
strMessage += "(" + stopWatch.Elapsed.Seconds + " Seconds)<br>what's up bro? ";
stopWatch.Restart();
LogHub.SendMessage(strMessage);
Thread.Sleep(2500);
strMessage += "(" + stopWatch.Elapsed.Seconds + " Seconds)<br>Call me soon ";
stopWatch.Restart();
LogHub.SendMessage(strMessage);
Thread.Sleep(3200);
strMessage += "(" + stopWatch.Elapsed.Seconds + " Seconds)<br>See you later ";
stopWatch.Restart();
LogHub.SendMessage(strMessage);
Thread.Sleep(1800);
strMessage += "(" + stopWatch.Elapsed.Seconds + " Seconds)<br>Okay man. See you ";
stopWatch.Restart();
LogHub.SendMessage(strMessage);
Thread.Sleep(2900);
strMessage += "(" + stopWatch.Elapsed.Seconds + " Seconds)<br>We're all Done!";
lblLog.Text = strMessage;
stopWatch.Stop();
}
}
}
This is my Startup1.cs file
using Microsoft.Owin;
using Owin;
using System;
using System.Threading.Tasks;
[assembly: OwinStartup(typeof(SignalR5.Startup1))]
namespace SignalR5
{
public class Startup1
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
And this is my LogHub.cs file
using Microsoft.AspNet.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SignalR5
{
public class LogHub : Hub
{
// This is the only one working, but I don't want to send to all clients.
public static void SendMessage(string strMessage)
{
var hub = GlobalHost.ConnectionManager.GetHubContext("LogHub");
hub.Clients.All.logMessage(strMessage);
}
}
}
As I mentioned, I’m having a real hard time figuring out how to just send this to the "caller" client. It works fine for "All".
2
Answers
When invoked your "Go" button you should invoke the "Go" method in the server, and then you just can send data to the
Caller
like:So for sending messages to a specific client, you’re gonna need their connection Id to identify which client you are going to send the message to.
I have made a demo here for sending messages to a specific user. This is the hub class. This send method will send messages to all connected clients like usual, but the extra thing I did is, register their connection id and user name at the beginning so that I can access them later.
This
SendToSingleUser
method is used for sending messages to a specific user, as for the dummy purpose I am just randomly taking the first client from the list. If you already know to which user you are going to send it, then you can pass the id to the method and look for the Id in your connection list just to make sure it’s a valid connection.The aspx file:
The full demo will be found here
Note: This demo is different than the example that you have posted, I just wanted to gave you an idea how you can achieve the communication.