SignalR 向特定客户端发送消息
SignalR send message to specific client
我有一个带有静态方法的集线器,可以向当前所有用户广播通知。
public class NotificationHub : Hub
{
private static string connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();
[HubMethodName("sendNotifications")]
public static void SendNotifications()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
context.Clients.All.updateNotifications();
}
}
我需要从前端获取一些文本框输入 javascript 例如10038 这是用户 ID 并将其发送到服务器 class 例如
public class NotificationRepository
{
private readonly string _connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
public IEnumerable<Notifications> GetNotifications(int userID)
{
var notifications = new List<Notifications>();
using (var connection = new SqlConnection(_connString))
{
connection.Open();
using (var command = new SqlCommand(String.Format(@"SELECT [ID], [UserID], [Message] FROM [dbo].[Notification] WHERE [UserID] = {0}", userID), connection))
{
command.Notification = null;
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
var reader = command.ExecuteReader();
while (reader.Read())
{
notifications.Add(item: new Notifications { ID = (int)reader["ID"], UserID = (int)reader["UserID"], Message = (string)reader["Message"] });
}
}
}
return notifications;
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
NotificationHub.SendNotifications();
}
}
}
我 return 一组由用户 ID 定义的针对特定用户的通知,但我似乎无法弄清楚如何映射响应,因此我只能 return 通知signalR 中的特定客户端。
如何将客户端的连接 ID 映射到输入的用户 ID?
我已经阅读了 signalR 文档,但它非常模糊,其他在线解决方案似乎也没有解决这个特定问题。
如有任何帮助,我们将不胜感激。
编辑
So far I have tried using IUserIdProvider and this is what I have so far but this does not seem to work.
On the front end I have set my queryString as follows
$.connection.hub.qs = "userId=" + $('#userText').val();
This is my custom IUserIdProvider
public class CustomUserIdProvider : IUserIdProvider
{
public string GetUserId(IRequest request)
{
return request.QueryString["userId"];
}
}
This is what my NotificationRepository looks like
public class NotificationRepository
{
private readonly string _connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
public IEnumerable<Notifications> GetUserNotifications(string userID)
{
var notifications = new List<Notifications>();
using (var connection = new SqlConnection(_connString))
{
connection.Open();
using (var command = new SqlCommand(String.Format(@"SELECT [ID], [UserID], [Message] FROM [dbo].[Notification] WHERE [UserID] = {0}", userID), connection))
{
command.Notification = null;
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler((sender, e) => dependency_OnChange(sender, e, userID));
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
var reader = command.ExecuteReader();
while (reader.Read())
{
notifications.Add(item: new Notifications { ID = (int)reader["ID"], UserID = (int)reader["UserID"], Message = (string)reader["Message"] });
}
}
}
return notifications;
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e, string userID)
{
if (e.Type == SqlNotificationType.Change)
{
NotificationHub.SendNotifications(userID);
}
}
}
And this is my hub
public class NotificationHub : Hub
{
private static string connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();
[HubMethodName("sendNotifications")]
public static void SendNotifications(string userId)
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
context.Clients.Client(userId).updateNotifications();
}
}
So I've figured out what the issue is in this scenario. When
initialising the queryString
$.connection.hub.qs = "userId=" + $('#userText').val();
You must do this before you start the connection to the hub.
我有一个带有静态方法的集线器,可以向当前所有用户广播通知。
public class NotificationHub : Hub
{
private static string connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();
[HubMethodName("sendNotifications")]
public static void SendNotifications()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
context.Clients.All.updateNotifications();
}
}
我需要从前端获取一些文本框输入 javascript 例如10038 这是用户 ID 并将其发送到服务器 class 例如
public class NotificationRepository
{
private readonly string _connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
public IEnumerable<Notifications> GetNotifications(int userID)
{
var notifications = new List<Notifications>();
using (var connection = new SqlConnection(_connString))
{
connection.Open();
using (var command = new SqlCommand(String.Format(@"SELECT [ID], [UserID], [Message] FROM [dbo].[Notification] WHERE [UserID] = {0}", userID), connection))
{
command.Notification = null;
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
var reader = command.ExecuteReader();
while (reader.Read())
{
notifications.Add(item: new Notifications { ID = (int)reader["ID"], UserID = (int)reader["UserID"], Message = (string)reader["Message"] });
}
}
}
return notifications;
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
NotificationHub.SendNotifications();
}
}
}
我 return 一组由用户 ID 定义的针对特定用户的通知,但我似乎无法弄清楚如何映射响应,因此我只能 return 通知signalR 中的特定客户端。
如何将客户端的连接 ID 映射到输入的用户 ID?
我已经阅读了 signalR 文档,但它非常模糊,其他在线解决方案似乎也没有解决这个特定问题。
如有任何帮助,我们将不胜感激。
编辑
So far I have tried using IUserIdProvider and this is what I have so far but this does not seem to work.
On the front end I have set my queryString as follows
$.connection.hub.qs = "userId=" + $('#userText').val();
This is my custom IUserIdProvider
public class CustomUserIdProvider : IUserIdProvider
{
public string GetUserId(IRequest request)
{
return request.QueryString["userId"];
}
}
This is what my NotificationRepository looks like
public class NotificationRepository
{
private readonly string _connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
public IEnumerable<Notifications> GetUserNotifications(string userID)
{
var notifications = new List<Notifications>();
using (var connection = new SqlConnection(_connString))
{
connection.Open();
using (var command = new SqlCommand(String.Format(@"SELECT [ID], [UserID], [Message] FROM [dbo].[Notification] WHERE [UserID] = {0}", userID), connection))
{
command.Notification = null;
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler((sender, e) => dependency_OnChange(sender, e, userID));
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
var reader = command.ExecuteReader();
while (reader.Read())
{
notifications.Add(item: new Notifications { ID = (int)reader["ID"], UserID = (int)reader["UserID"], Message = (string)reader["Message"] });
}
}
}
return notifications;
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e, string userID)
{
if (e.Type == SqlNotificationType.Change)
{
NotificationHub.SendNotifications(userID);
}
}
}
And this is my hub
public class NotificationHub : Hub
{
private static string connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();
[HubMethodName("sendNotifications")]
public static void SendNotifications(string userId)
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
context.Clients.Client(userId).updateNotifications();
}
}
So I've figured out what the issue is in this scenario. When initialising the queryString
$.connection.hub.qs = "userId=" + $('#userText').val();
You must do this before you start the connection to the hub.