locked
Uncaught TypeError: SendMessageByUser is not a function RRS feed

  • Question

  • User-1904414140 posted
    The methods that I create, not work..

       public void Connect(string userName)
            {
                var id = Context.ConnectionId;
    
    
                if (ConnectedUsers.Count(x => x.ConnectionId == id) == 0)
                {
                    ConnectedUsers.Add(new Model.UserDetail { ConnectionId = id, UserName = userName });
    
                    // send to caller
                    Clients.Caller.onConnected(id, userName, ConnectedUsers, CurrentMessage);
    
                    // send to all except caller client
                    Clients.AllExcept(id).onNewUserConnected(id, userName);
    
                }
    
            }
         
            public void SendMessageByUser(string userName, string message)
            {            
                Clients.All.messageReceived(userName, message);
            }


      $(function () {
            // Declare a proxy to reference the hub.
            var chatHub = $.connection.chatHub;
    
            registerClientMethods(chatHub);
    
            // Start Hub
            $.connection.hub.start().done(function () {
    
                registerEvents(chatHub)
            });
        });
    
        function registerEvents(chatHub) {
    
            chatHub.server.connect("Bruno2");
    
            $('#btnSendMsg').click(function () {
    
                var msg = $("#txtMessage").val();
                if (msg.length > 0) {
    
                    var userName = $('#hdUserName').val();
                    chatHub.server.SendMessageToAll(userName, msg);
                    $("#txtMessage").val('');
                }
            });
    
            $("#txtMessage").keypress(function (e) {
                if (e.which == 13) {
                    $('#btnSendMsg').click();
                }
            });
        }
    
    
    
    
    
    
        function registerClientMethods(chatHub) {
    
            // Calls when user successfully logged in
            chatHub.client.onConnected = function (id, userName, allUsers, messages) {
    
    
                $('#hdId').val(id);
                $('#hdUserName').val(userName);
                $('#spanUser').html(userName);
    
                // Add Existing Messages
                for (i = 0; i < messages.length; i++) {
    
                    AddMessage(messages[i].UserName, messages[i].Message);
                }
            }
    
            // On New User Connected
            chatHub.client.onNewUserConnected = function (id, name) {
    
                AddUser(chatHub, id, name);
            }
    
    
            // On User Disconnected
            chatHub.client.onUserDisconnected = function (id, userName) {
                $('#' + id).remove();
                var ctrId = 'private_' + id;
                $('#' + ctrId).remove();
                var disc = $('<div class="disconnect">"' + userName + '" logged off.</div>');
    
                $(disc).hide();
                $('#divusers').prepend(disc);
                $(disc).fadeIn(200).delay(2000).fadeOut(200);
    
            }
    
            chatHub.client.messageReceived = function (userName, message) {
    
                AddMessage(userName, message);
            }
        }
    
    
    
        function AddMessage(userName, message) {
            $('#chat').append('<li class="left clearfix">' +
                             '<span class="chat-img pull-left">' +
                                 '<img src="http://placehold.it/50/55C1E7/fff&text=U" alt="User Avatar" class="img-circle" />' +
                             '</span>' +
                             '<div class="chat-body clearfix">' +
                                 '<div class="header" style="text-align: left;">' +
                                     '<strong>' + userName + '</strong> <small class="pull-right text-muted">' +
                                         '16:12' +
                                     '</small>' +
                                 '</div>' +
                                 '<p>' +
                                     message +
                                 '</p>' +
                             '</div>' +
                         '</li>');
    
            var height = $('#chat')[0].scrollHeight;
            $('#chat').scrollTop(height);
        }


    Saturday, February 13, 2016 1:35 AM

All replies

  • User61956409 posted

    Hi brunopontes,

    According to your code, we could know “SendMessageByUser” is a hub method, I’d like to know when and where you call this hub method from your client side.

    chatHub.server.sendMessageByUser("username", "message");

    Best Regards,

    Fei Han

    Monday, February 15, 2016 2:33 AM