首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >connection.disconnected不是一个函数

connection.disconnected不是一个函数
EN

Stack Overflow用户
提问于 2019-08-24 18:15:14
回答 2查看 886关注 0票数 0

我很困惑:)

我正在使用带有Asp.Net核心的SignalR和一个JavaScript客户端。我只想自动检测断开和重新连接。

在谷歌了很多次之后,我想出了这个:

代码语言:javascript
复制
connection.disconnected(function() {
   setTimeout(function() {
       $.connection.hub.start();
   }, 5000); // Restart connection after 5 seconds.
});

但是我得到了一个错误:

代码语言:javascript
复制
connection.disconnected is not a function

这是我的整个JavaScript客户端:

代码语言:javascript
复制
 $(document).ready(function () {

    var divTimeStamp = document.getElementById("divTimeStamp");
    var img = document.getElementById('imgTest');
    var connection = new signalR.HubConnectionBuilder().withUrl("/NotificationUserHub").build();

    //Disable send button until connection is established
    document.getElementById("sendButton").disabled = true;

    connection.on("ReceiveMessage", function (user, image,timestamp ) {
        try {
            img.src = 'data:image/png;base64,' + image;
            divTimeStamp.innerText = timestamp;
        } catch (error) {
            console.error(error.toString());
        }
    });

   connection.disconnected(function() {
       setTimeout(function() {
           connection.start();
       }, 5000); // Restart connection after 5 seconds.
   });

    document.getElementById("sendButton").addEventListener("click", function (event) {
        var user = document.getElementById("userInput").value;
        var message = document.getElementById("messageInput").value;
        connection.invoke("MessageFromClient", user, message).catch(function (err) {
            return console.error(err.toString());
        });
        event.preventDefault();
    });
});

更改为以下内容:

代码语言:javascript
复制
function connection.disconnected(function () {

提供了以下内容:

我还尝试了这个:

代码语言:javascript
复制
connection.on("disconnect", function() { 
    setTimeout(function() { 
       connection.start(); 
 }, 5000); // Restart connection after 5 seconds. }); 

connection.on("disconnected", function() { 
    setTimeout(function() { 
       connection.start(); 
}, 5000); // Restart connection after 5 seconds. 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-08-24 19:05:24

使用微软推荐的方法如何?

代码语言:javascript
复制
async function start() {
    try {
        await connection.start();
        console.log("connected");
    } catch (err) {
        console.log(err);
        setTimeout(() => start(), 5000);
    }
};

connection.onclose(async () => {
    await start();
});

参考:https://docs.microsoft.com/en-us/aspnet/core/signalr/javascript-client?view=aspnetcore-2.2

票数 2
EN

Stack Overflow用户

发布于 2019-08-24 18:31:01

我想你叫错了,试试这个

代码语言:javascript
复制
connection.on("disconnect", function() {
   setTimeout(function() {
       $.connection.hub.start();
   }, 5000); // Restart connection after 5 seconds.
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57637005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档