Signalr错误:调用Hub方法完整js时出错

时间:2018-10-06 13:57:24

标签: javascript c# jquery asp.net-mvc signalr

存在控制台错误:

Error: There was an error invoking Hub method 'mail.Mail'.
at Object.error (jquery.signalR-2.3.0.js:179)
at init.callback (jquery.signalR-2.3.0.js:2717)
at hubConnection.fn.init.<anonymous> (jquery.signalR-2.3.0.js:2828)  
at hubConnection.fn.init.<anonymous> (jquery.signalR-2.3.0.js:799)
at hubConnection.fn.init.dispatch (jquery.min.js:3)
at hubConnection.fn.init.r.handle (jquery.min.js:3)
at Object.trigger (jquery.min.js:3)
at n.fn.init.triggerHandler (jquery.min.js:3)
at Object.triggerReceived (jquery.signalR-2.3.0.js:1504)
at WebSocket.connection.socket.onmessage (jquery.signalR-2.3.0.js:1803)

此方法服务器已触发

 chat.server.mail(msgCount, sendDate, shortMailMessage, fullName, friendId).fail(function (err) {
                    console.log(err);
                })

这是我的中心课程

 [HubName("mail")]
public class MailHub : Hub
{
public void Mail(int msgCount, DateTime sendDate, string shortMailMessage, 
string fullName, Guid friendId)
 {
    Clients.All.mailMessage(msgCount + 1, sendDate, shortMailMessage, 
 fullName, friendId);
 }
}

我的完整js代码:

   $(document).ready(function () {


        var profileOwnerId = '@Model.UserID';
        //setup hub class:
        var chat = $.connection.mail;
        console.log("signalr worked!");


        chat.client.mailMessage = function (msgCount, sendDate, shortMailMessage, fullName, friendId) {

            if ($('#messageNotificationCount').data("user") == profileOwnerId) {
                if (count > 0) {
                    $('#messageNotificationCount').css('display', 'inline-block');
                    $('#messageNotificationCount').text(count);
                }
            }

            var output = '<li><a href="/InboxUI/Index?friendId=' + friendId + '"><span class="photo"><img src="/assets/pages/img/avatars/default-profile-pic.png" class="img-circle" alt=""></span><span class="subject"><span class="from"> ' + fullName + ' </span><span class="time">' + sendDate.substr(0, 10).replace('-', '.').replace('-', '.') + '</span></span><span class="message"><b> ' + shortMailMessage + ' </b></span></a></li>';
            /*else{<span class="message"> ' + shortMailMessage + ' </span>}*/

            $('#messageNotificationItems').append(output);
        }

        $.connection.hub.error(function (error) {
            console.log('SignalR error: ' + error)
        });

        //signalR start

        $.connection.hub.start({ transport: ['webSockets', 'longPolling'] }).done(function () {


            console.log($.connection.hub.id);
            console.log("signalr start!");



    $('#sendMailMessage').click(function () {

        var mailMessage = {
            To: profileOwnerId,
            FullName: $('#YourFirstNameAndLastName').val().trim(),
            Email: $('#EMail').val().trim(),
            PhoneNumber: $('#PhoneNumber').val().trim(),
            Message: $('#Message').val().trim()
        }

        //null check

        if ((mailMessage.To != null && mailMessage.To != '' && mailMessage.To != undefined) && (mailMessage.FullName != null && mailMessage.FullName != '' && mailMessage.FullName != undefined) && (mailMessage.Email != null && mailMessage.Email != '' && mailMessage.Email != undefined) && (mailMessage.PhoneNumber != null && mailMessage.PhoneNumber != '' && mailMessage.PhoneNumber != undefined) && (mailMessage.Message != null && mailMessage.Message != '' && mailMessage.Message != undefined)) {

            $.ajax({
            url: '/UserUI/SendMailMessage',
            type: 'POST',
            data: mailMessage,
            success: function (response) {
                if (response.success) {
                    @*swal("@Html.Raw(Localization.Get("ThankYou"))", response.message, "success");*@

                   $('#YourFirstNameAndLastName').val("");
                   $('#EMail').val("")
                   $('#PhoneNumber').val("")
                   $('#Message').val("")
                }
                else
                {
                    swal("@Html.Raw(Localization.Get("Sorry"))", response.message, "warning");
                }
            },
            error: function () {
                swal("@Html.Raw(Localization.Get("Sorry"))", "", "warning");
            }
            }).done(function () {
                $.ajax({
                    type: 'POST',
                    url: '/InboxUI/GetMessageNotifications',
                    data: { friendId: profileOwnerId },
                    success: function (msgCount) {
                        messageCount = msgCount;
                    }
                }).done(function () {
                    //chat.server.messageNotify(profileOwnerId, messageCount);

                    mailMessage.Message = mailMessage.Message > 50 ? mailMessage.Message.substr(0, 50) : mailMessage.Message;
                    var date = new Date().toLocaleDateString();

                    var msgCount = messageCount;
                    var sendDate = date;
                    var shortMailMessage = mailMessage.Message;
                    var fullName = mailMessage.FullName;
                    var friendId = profileOwnerId;


                    //the real event takes place here

                    chat.server.mail(msgCount, sendDate, shortMailMessage, fullName, friendId).fail(function (err) {
                        console.log(err);
                    });

                });
            });

        }
            });

        });

    });

服务器方法未触发,我的中心类中的“邮件”方法,我该怎么办?

谢谢。

0 个答案:

没有答案