locked
SignalR does not send anything RRS feed

  • Question

  • User-741125216 posted

    hi,

    i have just started using SignalR, i created a test web application to play with signalR but it has no response.

    in the web tools developer's console it shows these two messages:

    SignalR: Websocket opened.

    SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332 and a connection lost timeout of 20000

    here is my hub code

    public class HubClass : Hub
        {
            public void Send()
            {
                for (int i = 0; i < 5; i++ )
                {
                    Clients.All.broadCastMessage(DateTime.Now.ToString());
                    Thread.Sleep(2000);
                }
                    
            }
        }

    and this is my jquery code:

    $(function () {
    
    
                var chat = $.connection.hubClass;
    
                chat.client.broadCastMessage = function (msg) {
    
                    console.log(msg);
    
                    $("#TimeStamp").append("<li>" + msg + "</li>");
    
                };
    
                console.log(chat);
    
                $.connection.hub.logging = true;
    
                $.connection.hub.start().done(function () {
    
                    console.log($.connection.hub.transport.name);
    
                });
    
    
            });

    thanks

    Monday, November 9, 2015 9:41 AM

Answers

  • User61956409 posted

    Hi a.tolba,

    $.connection.hub.start().done(function () { console.log($.connection.hub.transport.name); });

    Your HubClass and client-side code seems ok. But you don’t call send method after you start the connection.

    Besides, I create a simple sample to reproduce your problem, the code works fine on my side.

    Install this

    My hub class

    public class ChatHub : Hub
    {
        public void Send()
        {
            for (int i = 0; i < 5; i++)
            {
                Clients.All.broadCastMessage(DateTime.Now.ToString());
                Thread.Sleep(2000);
            }
        }
    
    }
    

    Web page

    <div class="container">
        <input type="text" id="message" />
        <input type="button" id="sendmessage" value="Send" />
        <ul id="discussion">
        </ul>
    </div>
    
    <script type="text/javascript">
        $(function () {
            var chat = $.connection.chatHub;
            chat.client.broadcastMessage = function (msg) {
                var encodedMsg = $('<div />').text(msg).html();
                $('#discussion').append('<li><strong>' + encodedMsg + '</li>');
            }; 
    
    
            $('#message').focus();
            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                    // Call the Send method on the hub. 
                    chat.server.send();
                    // Clear text box and reset focus for next comment.
                    $('#message').val('').focus();
                });
            });
        });
    </script>
    

    Besides, here is a tutorial, please refer to it.

    http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 10, 2015 12:18 AM

All replies

  • User61956409 posted

    Hi a.tolba,

    $.connection.hub.start().done(function () { console.log($.connection.hub.transport.name); });

    Your HubClass and client-side code seems ok. But you don’t call send method after you start the connection.

    Besides, I create a simple sample to reproduce your problem, the code works fine on my side.

    Install this

    My hub class

    public class ChatHub : Hub
    {
        public void Send()
        {
            for (int i = 0; i < 5; i++)
            {
                Clients.All.broadCastMessage(DateTime.Now.ToString());
                Thread.Sleep(2000);
            }
        }
    
    }
    

    Web page

    <div class="container">
        <input type="text" id="message" />
        <input type="button" id="sendmessage" value="Send" />
        <ul id="discussion">
        </ul>
    </div>
    
    <script type="text/javascript">
        $(function () {
            var chat = $.connection.chatHub;
            chat.client.broadcastMessage = function (msg) {
                var encodedMsg = $('<div />').text(msg).html();
                $('#discussion').append('<li><strong>' + encodedMsg + '</li>');
            }; 
    
    
            $('#message').focus();
            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                    // Call the Send method on the hub. 
                    chat.server.send();
                    // Clear text box and reset focus for next comment.
                    $('#message').val('').focus();
                });
            });
        });
    </script>
    

    Besides, here is a tutorial, please refer to it.

    http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 10, 2015 12:18 AM
  • User-741125216 posted

    Thank you that did it

    Tuesday, November 17, 2015 5:33 PM