当 websocket 连接打开时,PhantomJS 不会启动

PhantomJS doesn't start while websocket connection is open

我正在研究 slack 机器人,我遇到了一个奇怪的问题。我有一个使用 phantomJS 抓取网页的模块(通过它上面的 SpookyJS 和 CasperJS)。我编写了这个模块并从命令行手动测试了它运行。它运作良好。但后来我添加了 slackbots npm 模块,它抽象了 Slack 实时 API,并创建了一个带有 bot class 的模块。这个机器人模块需要我的模块和抓取代码(phantomJS)并在消息事件触发时调用它的函数:

var getAnswer = require('./getAnswer');

myBot.prototype._onMessage = function (message) {
    if (this._isChatMessage(message) &&
        this._isChannelConversation(message) &&
        this._isMentioningMe(message)) {

        this._reply(message);
    }
};

this._reply 基本上只是调用 getAnswer(originalMessage.text) 然后 self.postMessageToChannel(channel.name, reply, {as_user: true});

getAnswer returns 一个承诺,但它永远不会实现。我将 CasperJS 设置为 verbose 并发现在

之后没有任何反应
[info] [phantom] Starting...

一切都挂起...

我不知道如何解决这个问题。我猜这是因为 slackbots 模块在我调用 Bot.prototype.run 时建立了 websocket 连接。有什么建议吗?

正如我所说,我使用 Spooky 生成子 CasperJS 进程。我去了 Spooky 文档页面并阅读了这个:

Specifically, each Spooky instance spawns a child Casper process that runs a bootstrap script. The bootstrap script sets up a JSON-RPC server that listens for commands from the parent Spooky instance over a transport (either HTTP or stdio). The script also sets up a JSON-RPC client that sends events to the parent Spooky instance via stdout.

我使用 http 作为传输,但它不起作用,所以我将其更改为 stdio,它有所帮助。如果有人能解释一下为什么 它有帮助,我将不胜感激?