【问题标题】:Can we connect to the signalR hub on button click client side?我们可以在按钮单击客户端连接到 signalR 集线器吗?
【发布时间】:2018-02-08 04:30:12
【问题描述】:

我正在尝试通过按钮单击事件连接到集线器: 代码:

$("#send").on("click", function () {
            userName = ($("#fname").val()).toString();
            userName = userName.replace(/\s+/g, " ").trim();
            message = ($("#yourMessage").val()).toString();
            message = message.replace(/[\r\n]\s*/g, "");
            message = message.replace(/\s+/g, " ").trim();
            $("#yourMessage").val('');
            $("#send").attr('disabled', 'disabled');
            $("#fname").val(userName);
            if ($("#yourMessage").val() == "Start Typing...") {
                console.log("focus");
                $("#yourMessage").val("");
            }
            console.log(userName + " " + message);
            connectedHub(userName);
            
           
        });
var connectedHub=function(){
            var chatName = $("#fname").val();
            $.connection.hub.qs = "ChatName=" + chatName;
            $.connection.hub.start()
                .done(function () {
                    writeToPage("Somehow!");
                    //The Server side announce() is called here
                    myHub.server.serverAnnounce("Connected!!");
                    $.ajax({
                        url: "/Home/SendMessage?msg=" + message,
                        type: "post",
                        cache: false,
                        success: function (data) {
                            var data2 = JSON.stringify(data);
                            console.log("Success");
                            console.log("Server returned " + data2);
                        },
                        error: function (response) {
                            var err = JSON.stringify(response);
                            alert(err);
                        }

                    })
                .fail(function () {
                    writeToPage("Error!");
                })
                });
            }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

但是在点击事件中我得到了这个错误:

SelectChatType:207 Uncaught TypeError: Cannot read property 'hub' of undefined 在 connectedHub (SelectChatType:207) 在 HTMLInputElement。 (选择聊天类型:188) 在 HTMLInputElement.dispatch (VM104 jquery-3.3.1.js:5183) 在 HTMLInputElement.elemData.handle (VM104 jquery-3.3.1.js:4991)

但是,当退出“点击”事件功能时,此“集线器”会连接并正常工作。

如何在按钮单击事件上连接到服务器端集线器。基本上,我希望 OnConnected() 仅在单击按钮时触发。

【问题讨论】:

  • 我认为您错过了两个 signalR 文件,请再次查看
  • @DarshanDave 我已经在 _layout 中渲染了这些 scipts。这只是脚本的一部分。谢谢。
  • 好的,这可能是尝试放在同一页面上的问题
  • 您使用的是哪个信号器? core?
  • ASP.NET SignalR Core @NevilleNazerane

标签: jquery signalr signalr-hub


【解决方案1】:

这是我使用SignalR core 的方式。首先我创建了 hub 类:

public class Chat : Hub
{

    public const string Path = "Chat";

    public async Task Notify(int id) {
        await Clients.All.InvokeAsync("Notified", id);
    }

}

然后在我的创业公司中我使用了:

app.UseSignalR(routes =>
{
    routes.MapHub<Chat>(Chat.Path);
});

最后我用了下面的js:

    let connection = new signalR.HubConnection(socketURL + '/Chat.Path');

    connection.on('Notified', data => {
        console.log("Socket content:", data);
    });

    connection.start();

请注意,在我的情况下,Web 套接字服务器和客户端位于不同的网站上。这就是为什么我必须使用存储 url 的socketURL。您可以尝试不使用它。

【讨论】:

  • 我需要通过单击按钮连接到集线器。提前致谢。
猜你喜欢
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-08
  • 2019-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多