未捕获的InvalidStateError:无法在“WebSocket”上执行“send”:仍处于CONNECTING状态

t98cgbkg  于 2023-06-23  发布在  其他
关注(0)|答案(5)|浏览(373)

当我的页面加载时,我尝试向服务器发送send消息以启动连接,但它不起作用。这个脚本块在我的文件的顶部附近:

var connection = new WrapperWS();
connection.ident();
// var autoIdent = window.addEventListener('load', connection.ident(), false);

大多数时候,我看到标题中的错误:
未捕获的InvalidStateError:无法在“WebSocket”上执行“send”:仍处于CONNECTING状态
所以我尝试catch异常,正如你在下面看到的,但现在似乎InvalidStateError没有定义,并产生ReferenceError
下面是我的WebSocket连接的 Package 器对象:

// Define WrapperWS

function WrapperWS() {
    if ("WebSocket" in window) {
        var ws = new WebSocket("ws://server:8000/");
        var self = this;

        ws.onopen = function () {
            console.log("Opening a connection...");
            window.identified = false;
        };
        ws.onclose = function (evt) {
            console.log("I'm sorry. Bye!");
        };
        ws.onmessage = function (evt) {
            // handle messages here
        };
        ws.onerror = function (evt) {
            console.log("ERR: " + evt.data);
        };

        this.write = function () {
            if (!window.identified) {
                connection.ident();
                console.debug("Wasn't identified earlier. It is now.");
            }
            ws.send(theText.value);
        };

        this.ident = function () {
            var session = "Test";
            try {
                ws.send(session);
            } catch (error) {
                if (error instanceof InvalidStateError) {
                    // possibly still 'CONNECTING'
                    if (ws.readyState !== 1) {
                        var waitSend = setInterval(ws.send(session), 1000);
                    }
                }
            }
        window.identified = true;
            theText.value = "Hello!";
            say.click();
            theText.disabled = false;
        };

    };

}

我在Ubuntu上使用Chromium进行测试。

quhf5bfb

quhf5bfb1#

您可以通过等待readyState为1的代理函数发送消息。

this.send = function (message, callback) {
    this.waitForConnection(function () {
        ws.send(message);
        if (typeof callback !== 'undefined') {
          callback();
        }
    }, 1000);
};

this.waitForConnection = function (callback, interval) {
    if (ws.readyState === 1) {
        callback();
    } else {
        var that = this;
        // optional: implement backoff for interval here
        setTimeout(function () {
            that.waitForConnection(callback, interval);
        }, interval);
    }
};

然后使用this.send代替ws.send,并将之后应该运行的代码放在回调中:

this.ident = function () {
    var session = "Test";
    this.send(session, function () {
        window.identified = true;
        theText.value = "Hello!";
        say.click();
        theText.disabled = false;
    });
};

对于更精简的东西,你可以看看promises

izj3ouym

izj3ouym2#

出现此错误的原因是您在WebSocket连接建立之前发送消息
你可以通过简单地这样做来解决它:

conn.onopen = () => conn.send("Message");

onopen函数等待您的WebSocket连接建立后再发送消息。

gjmwrych

gjmwrych3#

如果你使用一个WebSocket客户端对象并从随机应用程序位置连接,那么对象可以处于连接模式(并发访问)。
如果您只想通过一个websocket进行交换,那么请使用promise创建类并将其保存在属性中

class Ws {
  get newClientPromise() {
    return new Promise((resolve, reject) => {
      let wsClient = new WebSocket("ws://demos.kaazing.com/echo");
      console.log(wsClient)
      wsClient.onopen = () => {
        console.log("connected");
        resolve(wsClient);
      };
      wsClient.onerror = error => reject(error);
    })
  }
  get clientPromise() {
    if (!this.promise) {
      this.promise = this.newClientPromise
    }
    return this.promise;
  }
}

创建单例

window.wsSingleton = new Ws()

在应用程序的任何位置使用clientPromise属性

window.wsSingleton.clientPromise
  .then( wsClient =>{wsClient.send('data'); console.log('sended')})
  .catch( error => alert(error) )

http://jsfiddle.net/adqu7q58/11/

2sbarzqh

2sbarzqh4#

方法一:检查连接

你可以在socket连接时解析promise:

async function send(data) {
    await checkConnection();
    ws.send(data);
}
实现

这个技巧是使用解析器数组来实现的。

let ws = new WebSocket(url);

let connection_resolvers = [];
let checkConnection = () => {
    return new Promise((resolve, reject) => {
        if (ws.readyState === WebSocket.OPEN) {
            resolve();
        }
        else {
            connection_resolvers.push({resolve, reject});
        }
    });
}

ws.addEventListener('open', () => {
    connection_resolvers.forEach(r => r.resolve())
});

方法二:等待连接

当socket未连接时,可以解析promise:

const MAX_RETRIES = 4;
async function send(data, retries = 0) {
    try {
        ws.send(data);
    } 
    catch (error) {
        if (retries < MAX_RETRIES error.name === "InvalidStateError") {
            await waitForConnection();
            send(data, retries + 1);
        }
        else {
            throw error;
        }
    }
}
实现

这个技巧是使用解析器数组实现的。

let ws = new WebSocket(url);

let connection_resolvers = [];
let waitForConnection = () => {
    return new Promise((resolve, reject) => {
        connection_resolvers.push({resolve, reject});
    });
}

ws.addEventListener('open', () => {
    connection_resolvers.forEach(r => r.resolve())
});
    • 我的看法**是,第二种方法的性能稍微好一点!
6rvt4ljy

6rvt4ljy5#

可以将函数和readyState与setTimeout一起使用。

function openSocket()
{
    webSocket = new WebSocket("");
}
function sendData()
{
    if(webSocket.readyState)
    {
        webSocket.send(JSON.stringify(
        {
            "event"     : "", 
            "message"   : ""
        }));
    }
    else
    {
        setTimeout(sendData, 1000);
    }
}
function eventHandler()
{
    webSocket.onmessage = function(e)
    {
        data  = JSON.parse(e.data);
        event = data.event;

        switch (event)
        {...}
    }
}

openSocket();
sendData();
eventHandler();

相关问题