javascript - 未捕获的无效状态错误 : Failed to execute 'send' on 'WebSocket' :

标签 javascript websocket

我正在尝试运行此代码:

function smConnect() {
  ws = new WebSocket('ws://127.0.0.1:1805/');
  delete ws.URL;

  ws.onopen = function(response) {};
  ws.onmessage = function(response) {};
  ws.onclose = function(response) {};
  ws.onerror = function(error) {};
}

smConnect();
ws.send('message', 'hi');

但它返回给我这个错误:

Uncaught InvalidStateError: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.

可能是什么问题?

最佳答案

你可以这样做,它添加了一些日志记录,发送你可以在构造函数之外处理的信息,并且你还可以在它自己的命名空间中抽象出 SocketWrapper (好吧,是的,它现在在窗口中:))

您可以检查开发工具(大多数浏览器中的 F12)以查看日志/错误中发生的情况,例如:这里抛出错误,因为没有可用的套接字:)

并且您不需要为所有事件提供值,只需为您需要的事件提供值(您的情况是 onopen + 也许是 onmessage?)

(在定义的回调中,这将指向套接字,而不是 SocketWrapper,SocketWrapper 也不提供 ws 变量,它是私有(private)的,但我猜应该这样做)

SocketWrapper 上有一个发送方法,当您发送到一个关闭的流时,它会抛出一个错误,但如果它尚未打开,它会将消息排队直到它被打开,然后清空队列以websocket(所以从某种意义上说,您不需要设置 onopen 回调,只需使用 send 方法添加它就可以了;)

(function(nameSpace) {
  function createMethod(method, options, stateCallback) {
    var that = this;
    this[method] = function() {
      if (stateCallback && stateCallback.apply) {
        stateCallback(method);
      }
      console.info(method);
      if (options[method] && options[method].apply) {
        options[method].apply(that, arguments);
      }
    };
  }

  function SocketWrapper(options) {
    var ws,
      events = ['onopen', 'onmessage', 'onclose', 'onerror'],
      i, len, prop = {
        opened: false,
        closed: false,
        error: false
      },
      method;

    if (typeof options === 'undefined' || !options) {
      throw 'ArgumentException: please add default constructor options';
    }
    
    this.queue = [];
    
    this.onEventTrigger = function(eventName) {
      var i, len;
      if (eventName === 'onopen') {
        prop.opened = true;
        prop.closed = false;
        // openend send queue
        if (this.queue.length > 0) {
          for (i = this.queue.length; --i >= 0;) {
            this.send.apply(this, this.queue[0]);
            this.queue.splice(0, 1);
          }
        }
      }
      if (eventName === 'onerror') {
        prop.error = true;
      }
      if (eventName === 'onclosed') {
        prop.opened = false;
        prop.closed = true;
      }
    };

    this.init = function() {
      var cb = this.onEventTrigger.bind(this);
      ws = new WebSocket(options.url);

      for (i = 0; i < events.length; i++) {
        method = events[i];
        createMethod.apply(ws, [method, options, cb]);
      }
    };

    this.send = function() {
      if (prop.closed) {
        throw 'InvalidOperation: Cannot send messages to a closed Websocket!';
      }
      if (!prop.opened) {
        this.queue.push(arguments);
      } else {
        ws.send.apply(ws, arguments);
      }
    };
    
    this.init();
    return this;
  }

  window.SocketWrapper = SocketWrapper;
}(window));

var socket = new window.SocketWrapper({
  url: 'ws://127.0.0.1:1805',
  onopen: function() {
    this.send('message', 'hi');
  },
  onmessage: function() {
    console.log(arguments);
  },
  onclose: function() {
    socket = null;
  },
  onerror: function() {
    console.log('error occured, oh no!');
    console.error(arguments);
  }
});
socket.send('i am message send to soon, but since i check the state of the ws object, i will be queued and send when appropriate');

关于javascript - 未捕获的无效状态错误 : Failed to execute 'send' on 'WebSocket' :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29157807/

相关文章:

go - 不能在 websocket.Message.Send 的参数中使用 ccc(int 类型)作为 *websocket.Conn 类型

javascript - 将内容丰富的 CMS 与 gatsby 页面连接起来

javascript - 动态数据打印

python - 使用全局变量的 WebSocket 线程

javascript - 使用 Express 和 NodeJS 构建 REST API 的最佳实践

javascript - 发送新的错误 ('Can\' t 设置 header 。');

javascript - 如何解决 Chrome 控制台中的 SyntaxError?

javascript - 属性的类别属性

javascript - 使用 dojo 修改 div 的文本

java - Tomcat 8.0.x Websockets、SSL 和 Windows --> 最后一帧延迟