最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "Cannot switch to old mode now" - Node.JS apn module error in tls.connect function - Stack Overfl

programmeradmin2浏览0评论

I am trying to implement Node.JS apn module to connect to APNS (Apple Push Notification service) to push notification to iPhone devices from the Node server (using ExpressJS) hosted on Amazon EC2 instance running Ubuntu 12.04. I am getting this error:

"Error: Cannot switch to old mode now.",
"    at emitDataEvents (_stream_readable.js:720:11)",
"    at ReadStream.Readable.resume (_stream_readable.js:705:3)",
"    at TLSSocket.<anonymous> (/home/ubuntu/usemebeta/routes/message.js:101:48)",
"    at TLSSocket.g (events.js:186:14)",
"    at TLSSocket.EventEmitter.emit (events.js:97:17)",
"    at TLSSocket.<anonymous> (_tls_wrap.js:579:16)",
"    at TLSSocket.EventEmitter.emit (events.js:97:17)",
"    at TLSSocket._finishInit (_tls_wrap.js:198:8)"

when I call the apn module function to push notification. However when I do the same thing in a function written in a file and execute that file, then it works just fine. Please help. How should I go about fixing the problem?

I am trying to implement Node.JS apn module to connect to APNS (Apple Push Notification service) to push notification to iPhone devices from the Node server (using ExpressJS) hosted on Amazon EC2 instance running Ubuntu 12.04. I am getting this error:

"Error: Cannot switch to old mode now.",
"    at emitDataEvents (_stream_readable.js:720:11)",
"    at ReadStream.Readable.resume (_stream_readable.js:705:3)",
"    at TLSSocket.<anonymous> (/home/ubuntu/usemebeta/routes/message.js:101:48)",
"    at TLSSocket.g (events.js:186:14)",
"    at TLSSocket.EventEmitter.emit (events.js:97:17)",
"    at TLSSocket.<anonymous> (_tls_wrap.js:579:16)",
"    at TLSSocket.EventEmitter.emit (events.js:97:17)",
"    at TLSSocket._finishInit (_tls_wrap.js:198:8)"

when I call the apn module function to push notification. However when I do the same thing in a function written in a file and execute that file, then it works just fine. Please help. How should I go about fixing the problem?

Share Improve this question edited Apr 20, 2015 at 2:03 mklement0 438k68 gold badges701 silver badges911 bronze badges asked Aug 19, 2013 at 5:34 ajayajay 9,6809 gold badges46 silver badges72 bronze badges 2
  • 1 Show the relevant code, so that we can reproduce and correct the error. – user568109 Commented Aug 19, 2013 at 8:16
  • 1 Also what version of Node.js are you using? – Miroslav Bajtoš Commented Aug 19, 2013 at 9:37
Add a comment  | 

2 Answers 2

Reset to default 20

Update: The behavior described below may have changed since this answer was originally written; as of 0.12.3, the docs suggest that you can now switch back and forth between paused (new) and flowing (old) mode, and that streams start out in paused mode.
Also, "old" mistakenly suggests a deprecated way of doing things, but while the flowing mode came first, both are supported and have their uses.


I don't have a specific answer, but a general explanation:

The short of it: old code that calls .resume() or .pause() on readable streams can break on node 0.10 and higher.

The long of it: You see this error on node 0.10 or higher when a readable stream has been initialized to use the new, paused mode (based on the readable event) and an attempt is later made to switch to the old, flowing mode (based on the data event), which is implicitly attempted when you call .resume() or .pause() - see the docs.

Setting up a readable event may not even be directly involved. For instance, it seems that using the .pipe() method now implicitly switches to the new, paused mode, so a subsequent .resume() call would trigger this error.

Not sure if this is the 'correct' way to fix the issue, but it worked for me. (You'll need to have brew)

  1. brew uninstall node
  2. brew install nvm
  3. copy this into your shell config (in my case .zshrc)

export NVM_DIR=~/.nvm source $(brew --prefix nvm)/nvm.sh

  1. nvm install iojs
发布评论

评论列表(0)

  1. 暂无评论