Соединение ReactJS, MVC и SignalR не определено - PullRequest
0 голосов
/ 03 марта 2019

ПРИВЕТ. Я изучал SignalR, ReactJS и MVC .... Я пытался создать приложение для чата, в нем были некоторые проблемы ... поделился кодом ниже Класс My ChatHub.cs

    public class ChatHub : Hub
    {
        public void Send(string newName, string newComment, string currentUserId, string friendUserId)
        {
                // Calling the addNewMessageToPage method to update clients.
                Clients.All.addNewMessageToPage(newName, newComment, currentUserId, friendUserId);
        }
    }

В моем классе Startup.cs я добавил app.MapSignalR () ... Мой ReactJS Comment.cshtmlfile ... Я добавил следующие скрипты. В моей функции handleSubmit мой код работал нормально до добавления кода signalR, который называется "var chat".= $ .connection.chatHub "Эта строка дает чату неопределенное значение.Скажите пожалуйста что я делаю не так

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.signalR-2.4.0.min.js"></script>
<script src="~/signalr/hubs"></script>
handleSubmit(e) {
        e.preventDefault();

        var updatedData = {
            CComment: this.state.CComment,
            UName: this.state.UName,
            CUId: this.state.CUId,
            CFId: this.state.CFId
        };

        $.ajax({
            type: "POST",
            url: this.props.url,
            data: { myData: JSON.stringify(updatedData)},
            success: function(response){
                var newresp = JSON.parse(response);
                var newName =  newresp.UName;
                var newComment = newresp.CComment;
                var currentUserId = newresp.CUId;
                var friendUserId  = newresp.CFId;
                $(function () { 
                        var chat = $.connection.chatHub; // error is coming here... connection is undefined
                        $.connection.hub.start().done(function () {  
                            chat.server.send(newName, newComment, currentUserId, friendUserId); 
                        });
                        chat.client.addNewMessageToPage = function (newName, newComment, currentUserId, friendUserId) { 
                        //Some function i will perform...
                };
                });
            }
        });
...