小编Bal*_*ory的帖子

SignalR向特定客户端发送消息

我有一个带静态方法的集线器,它向当前所有用户广播通知.

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要从前端javascript中获取一些文本框输入,例如10038,这是userID并将其发送到服务器类,例如

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 …
Run Code Online (Sandbox Code Playgroud)

c# asp.net signalr signalr-hub

5
推荐指数
1
解决办法
6184
查看次数

标签 统计

asp.net ×1

c# ×1

signalr ×1

signalr-hub ×1