Click here to Skip to main content
15,878,945 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
[HubName("chatHub")]
   public class ChatHub : Hub
   {
       private readonly int TimeoutInSeconds = 30;
       private readonly Chat _chat;



       public ChatHub()
           : this(Chat.Instance)
       {
       }

       public ChatHub(Chat chat)
       {
           _chat = chat;
       }

       public void Join(string myName)
       {
           if (Chat.Clients.Where(x => x.Name.Equals(myName)).Count().Equals(0))
           {
               Chat.Clients.Add(new Client() { Name = myName, LastResponse = DateTime.Now });
               SendMessageServer(string.Format("{0} entered the chat", myName));
               GetClients();
               Caller.Naam = myName;
           }
           else
               throw new Exception("This login is allready in use");
       }

       private void GetClients()
       {
           _chat.GetClients();
       }

       public void SendMessageServer(string message)
       {
           _chat.SpreadMessage(message);
       }

       public void SendMessage(string message)
       {
           if (Chat.Clients.Where(x => x.Name.Equals(Caller.Naam)).Count() > 0)
               _chat.SpreadMessage(string.Format("({0}) <b>{1}</b>: {2}", DateTime.Now.ToString("HH:mm"), Caller.Naam, message));
       }
   }

JavaScript
$(function () {
    var chatHubClient = $.connection.chatHub;


    // Send a new message to the server
    $("#SendMessage").click(function () {
        var message = $('#textMessage').val();
        chatHubClient.sendMessage(message);
        $('#textMessage').val("");
    });

    // Start the connection
    $.connection.hub.start(function () {
        chatHubClient.join('@Model.Name');
    });

    // Receive a new message from the server
    chatHubClient.newMessage = function (message) {
        message = $("#chatWindow").html() + "<br />" + message;
        $("#chatWindow").html(message);
    };

    // Receive the new active userlist
    chatHubClient.userList = function (message) {
        message = JSON.parse(message);
        var options;
        $.each(message, function (index) {
            options += '<option value="' + message[index].Name + '">' + message[index].Name + '</option>';
        });
        $("select#users").html(options);
    }
});
Posted
Updated 1-Nov-14 23:23pm
v2
Comments
Kornfeld Eliyahu Peter 2-Nov-14 5:23am    
And your problem is?
Mich1309 2-Nov-14 5:24am    
i want make test to connect into 1000 hubs locally in same time
Kornfeld Eliyahu Peter 2-Nov-14 5:28am    
There is no problem (on client or server) to have multiple hubs - all you need is create a class (on server) for every hub and create connection method (on client) for every hub you want to communicate with...
Mich1309 2-Nov-14 5:29am    
can you show me an example please?
Kornfeld Eliyahu Peter 2-Nov-14 5:42am    
Google is your friend - http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server#multiplehubs

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900