How to get a list of connected clients in SignalR

I am new to SignalR. My first assignment is to make a simple chat application.

I browsed and read, and finally made a page where you come and chat, and it works great.

Now I need to show a list of connected clients. For this, I wrote the following code. This is my hub.

public class ChatHub: Hub { chatEntities dc = new chatEntities(); public void Send(string message,string clientName) { Clients.addMessage(message,clientName); } // I want to save the user into my database, when they join public void Joined(string userId,string userName) { CurrentChattingUsers cu = new CurrentChattingUsers(); cu.ConnectionId = userId; cu.UserName = userName; dc.CurrentChattingUsers.AddObject(cu); dc.SaveChanges(); Clients.joins(userId, userName); } // This will return a list of connected user from my db table. public List<ClientModel> GetConnectedUsers() { var query = (from k in dc.CurrentChattingUsers select new ClientModel() { FullName = k.UserName, UserId = k.ConnectionId }).ToList(); return query; } } 

And here it is ... Now what ??? Am I going in the right direction? If, then what do I call these methods from the view? Some good suggestions will really help me. greetings

EDIT:

I added the following script when the elementary student

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

This is a method that returns customer names in my hub

  public List<ClientModel> GetConnectedUsers() { var data = (from k in dc.Users select new ClientModel() { FullName = k.UserName }).ToList(); Clients.loadUsers(data); return data; } 

in firebug, I see that it returns something like this:

 {"State":{},"Result":[{"FullName":"mokarom","UserId":null}, {"FullName":"aka8000","UserId":null},{"FullName":"johnnyno5","UserId":null},{"FullName":"reza","UserId":null},{"FullName":"amyo","UserId":null},{"FullName":"rezatech","UserId":null}],"Id":"0","Error":null,"StackTrace":null} 

But how would I display this, in my opinion?

EDIT:

this is the full view so far

 <script type="text/javascript"> var chat; var myClientName $(document).ready(function(){ myClientName = '@Request.Cookies["ChatterName"].Value'; // Created proxy chat = $.connection.chatHub; // Assign a function to be called by the server chat.addMessage = onAddMessage; // Register a function with the button click $("#broadcast").click(onBroadcast); $('#message').keydown(function (e) { if (e.which == 13) { //Enter e.preventDefault(); onBroadcast(); } }); // Start the connection $.connection.hub.start(function () { chat.getConnectedUsers(); }); chat.loadUsers = function (data) { loadUsers(data); }; }); function onAddMessage(message,clientName) { // Add the message to the list $('#messages').append('<div class="chatterName">' + clientName + ' </div><div class="chatterMessage"> ' + message + '</div><div class="clear">'); } function onBroadcast() { // Call the chat method on the server chat.send($('#message').val(), myClientName); $('#message').val(''); } function loadUsers(data) { $('#clientList').html(data.Result[0].FullName); } </script> 

Problem: they don’t see anything here: $ ('# clientList'). html (data.Result [0] .FullName); firebug says "data not defined"

+8
asp.net-mvc signalr
source share
1 answer

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 the hub is up, chats will have all the public functions of your hub available as javascript functions. This is what creates signal / hubs using the best available connection method between client and server.

Otherwise, your C # hub will have access to any javascripts functions that you configure, for example.

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

ps - you could also consider using OnConnectedAsync , although of course you can still save them. I am also waiting for full support for web farm support using sql, which is in the pipeline.

+6
source share

All Articles