简体   繁体   English

Node.js和Socket.io-无法读取未定义的属性“ on”

[英]Node.js & Socket.io - Cannot read property 'on' of undefined

I have run into an error when establishing a websocket connection using socket.io and node.js. 使用socket.io和node.js建立Websocket连接时遇到错误。 Here is my environment: 这是我的环境:

  • OS: Windows 7 作业系统:Windows 7
  • Node: 6.9.2 节点:6.9.2
  • NPM: 4.0.5 NPM:4.0.5
  • Packages: 包装方式:
    • "express": "^4.14.0" “表达”:“ ^ 4.14.0”
    • "socket.io": "1.7.1" “ socket.io”:“ 1.7.1”
    • "socket.io-client": "1.7.1" “ socket.io-client”:“ 1.7.1”
    • "ws": "1.1.1" “ ws”:“ 1.1.1”

When a websocket connection is attempted the following error is thrown: 尝试进行websocket连接时,将引发以下错误:

TypeError: Cannot read property 'on' of undefined
    at PerMessageDeflate.decompress (C:\ng-project\node_modules\ws\lib\PerMessageDeflate.js:242:37)
    at Receiver.applyExtensions (C:\ng-project\node_modules\ws\lib\Receiver.js:362:54)
    at C:\ng-project\node_modules\ws\lib\Receiver.js:508:14
    at Receiver.flush (C:\ng-project\node_modules\ws\lib\Receiver.js:347:3)
    at Receiver.opcodes.1.finish (C:\ng-project\node_modules\ws\lib\Receiver.js:541:12)
    at Receiver.expectHandler (C:\ng-project\node_modules\ws\lib\Receiver.js:493:33)
    at Receiver.add (C:\ng-project\node_modules\ws\lib\Receiver.js:103:24)
    at Socket.realHandler (C:\ng-project\node_modules\ws\lib\WebSocket.js:825:20)
    at ZoneDelegate.invokeTask (C:\ng-project\node_modules\zone.js\dist\zone-node.js:265:35)
    at Zone.runTask (C:\ng-project\node_modules\zone.js\dist\zone-node.js:154:47)
    at Socket.ZoneTask.invoke (C:\ng-project\node_modules\zone.js\dist\zone-node.js:335:33)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:177:18)
    at Socket.Readable.push (_stream_readable.js:135:10)
    at TCP.onread (net.js:542:20)

Looking into the ws module I can see the line of code throwing the error: 查看ws模块,我可以看到引发错误的代码行:

this._inflate.on('error', onError).on('data', onData);

_inflate is a zlib InflateRaw instance created by zlib.createInflateRaw({...}) a few lines earlier. _inflatezlib.createInflateRaw({...})前面创建的zlib InflateRaw实例。 The problem is that the InflateRaw#on function returns undefined in my environment. 问题是InflateRaw#on函数在我的环境中返回undefined

I have tried installing alternate versions of Node to work around this without any luck... Altering the ws package is not an acceptable workaround for me. 我尝试安装替代版本的Node来解决此问题,但没有任何运气...更改ws软件包对我来说不是可接受的解决方法。

EDIT 1: Adding source code which throws the error. 编辑1:添加引发错误的源代码。

io.on('connection', (socket) => {
  // Listen for verification subscription requests
  log.debug("Established socket connection " + socket.id);
  socket.on('verify subscribe', (msg) => {
    log.debug("Recieved 'verify subscribe' message: " + JSON.stringify(msg));
    // Do stuff....
  });
});

This code outputs the following log messages prior to the error: 该代码在错误之前输出以下日志消息:

[2016-12-30T19:08:09.219Z] DEBUG: Established socket connection -g9oZM6pEqpFyU_BAAAA
[2016-12-30T19:08:09.275Z] DEBUG: Recieved 'verify subscribe' message: {"token":"short-ish.token","authorization":"long.auth-token"}

This issue was discovered to be a bug with zone.js and has been fixed in later versions (>0.7.0). 发现此问题是zone.js的错误,已在更高版本(> 0.7.0)中修复。 The issue is tracked on GitHub here: angular/zone.js#508 . 在GitHub上可以在以下位置跟踪该问题: angular / zone.js#508

You io is not set yet. io尚未设定。 Make sure you have done 确保您已完成

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

From the docs chat example - http://socket.io/get-started/chat/ 从文档聊天示例-http: //socket.io/get-started/chat/

If you don't setup io it will be undefined . 如果您不设置io ,它将是undefined

Also see: http://socket.io/docs/# 另请参阅: http : //socket.io/docs/#

var app = require('http').createServer(handler)
var io = require('socket.io')(app);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM