How to get a list of connected clients on SignalR

心已入冬 提交于 2019-12-05 04:08:57

JavaScript

var chats = $.connection.chatHub;
chats.loadUsers = function (data) { loadUsers(data); };
var connectedUserCount = 0;

$.connection.hub.start(function () 
         { chats.getConnectedUsers(); });

function loadUsers =  = function (data) {
  console.log(data); //so you can see your data in firebug.etc
            //which signal r will have converted to json so you could try
  var numberOfUsers = data.length;  
}

Once hub is started chats would have all the public functions of your hub available as javascript functions. This is what the signalr/hubs creates using the best available connection method between client and server.

In reverse your C# hub will have access to any javascripts functions you setup, e.g.

Clients.loadUsers(query);  
//add this to you server side just before you return query

ps - you might also consider using OnConnectedAsync, though of course you might still persist these. I'm also waiting for full support for web farm support using sql, which is in the pipeline.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!