我从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);
}


我相信这是您在控制台中看到的错误的根本原因。如果您向我们展示您正在绑定或与您的websocket一起使用的事件/回调/处理程序有关的代码,以便确定不使用DOM Level 2事件的方式,将对您有所帮助。

关于javascript - websockets/ws TypeError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31543833/

10-13 01:26