websockets/ws 类型错误

websockets/ws TypeError

我收到来自 websockets/ws 的错误:

/root/diag/node_modules/ws/lib/WebSocket.js:422
    event.target = target;
                 ^
TypeError: Cannot assign to read only property 'target' of reserved fields (2, 3) must be empty
    at WebSocket.onError (/root/diag/node_modules/ws/lib/WebSocket.js:422:18)
    at WebSocket.emit (events.js:110:17)
    at Receiver.onerror (/root/diag/node_modules/ws/lib/WebSocket.js:832:10)
    at Receiver.error (/root/diag/node_modules/ws/lib/Receiver.js:317:8)
    at Receiver.processPacket (/root/diag/node_modules/ws/lib/Receiver.js:186:12)
    at Receiver.add (/root/diag/node_modules/ws/lib/Receiver.js:91:24)
    at firstHandler (/root/diag/node_modules/ws/lib/WebSocket.js:767:22)
    at process._tickCallback (node.js:355:11)

这不是指我的代码中的任何特定行,所以我什至不知道我的代码中的什么地方导致了这个问题。

这是模块新更新的错误还是我做错了什么?

我有一段时间没有更新 websocket 客户端(这是错误的来源)。

编辑

websocket 在两个不同的位置使用:

// main file that then passes socket to libdiag.js
socket.on('connect', function () {
    socket.emit('authenticate', {
        method: 'token',
        credentials: token
    });
});

socket.on('authenticate', function(packet) {
    var data = JSON.parse(packet.data);
    if (packet.channel == CHANNEL && data.status == "authenticated") {
        libdiag.start(socket, CHANNEL);
    }
});

socket.on('disconnect', function () {
    start();
});

socket.on('error', function (err) {
    libdiag.error(err); // This is just a logger that writes to file
});

还有这个文件

// This is libdiag.js
socket.on('data', function (packet) {
    var data = JSON.parse(packet.data);

    // New listener is requesting handshake
    if (packet.type == SOCKET_HANDSHAKE) {
        _handshake(data);
    }

    // A listener has left
    if (packet.type == SOCKET_CLOSED) {
        _listenerLeft(data);
    }

    if (packet.type == SOCKET_UPDATE) {
        _read(data);
    }
});

socket.on('disconnect', function () {
    libdiag.destroy(); // This calls socket.disconnect() and sets the variables to null
});

我相信您正在尝试将 DOM Level 2 绑定到您的一个 websockets 回调,DOM 2 级事件的 target 属性 设置为 readOnly:

// Introduced in DOM Level 2:
interface Event {

  // PhaseType
  const unsigned short      CAPTURING_PHASE                = 1;
  const unsigned short      AT_TARGET                      = 2;
  const unsigned short      BUBBLING_PHASE                 = 3;

  readonly attribute DOMString        type;
  readonly attribute EventTarget      target;
  readonly attribute EventTarget      currentTarget;
  readonly attribute unsigned short   eventPhase;
  readonly attribute boolean          bubbles;
  readonly attribute boolean          cancelable;
  readonly attribute DOMTimeStamp     timeStamp;
  void               stopPropagation();
  void               preventDefault();
  void               initEvent(in DOMString eventTypeArg, 
                               in boolean canBubbleArg, 
                               in boolean cancelableArg);
};

似乎在您的流程中,某些东西正在触发 websocketonError 处理程序,它试图更改 event.target:

function onError (event) {
    event.type = 'error';
    event.target = target;
    listener.call(target, event);
}

我相信这是您在控制台中看到的错误的根本原因。如果您向我们展示有关 events/callbacks/handlers 的代码将会很有帮助,您正在绑定或使用您的 websocket 以确定不使用 DOM 2 级事件的方法。