1

在我的代码中,我有一个 socket.io 服务器,我需要客户端向服务器发送信号,然后启动 spooky ... 启动 spooky 后,我需要通过 io 从服务器到客户端来回发送一些数据,所以我需要 socket.io幽灵内部的实例

这是我的代码的简化版本

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

try {
    var Spooky = require('spooky');
} catch (e) {
    var Spooky = require('../lib/spooky');
}



var server = http.createServer(app).listen(1300);
 var io  = socket.listen(server);

io.on('connection' , function(socket){

    socket.on('start_spooky' , function (data) {
        io.emit('push_msg' , {text : 'starting spooky ....  '  });
        run_spooky(io);
    })

});



function run_spooky(io){

    var spooky = new Spooky({
        child: {
            transport: 'http'
        },
        casper: {
            logLevel: 'debug',
            verbose: true
        }
    }, function (err) {
        if (err) {
            e = new Error('Failed to initialize SpookyJS');
            e.details = err;
            throw e;
        }
        spooky.start( 'http://localhost');

        spooky.then([{
            io: io
        }, function () {
            io.emit('push_msg' , {text : 'web page is loaded ...  ' });
        }]);

        spooky.run();
    });



}

显然这不适用于 io 实例

    spooky.then([{
        io: io
    }, function () {
        io.emit('push_msg' , {text : 'web page is loaded ...  ' });
    }]);

我得到错误

无法将请求序列化为 JSON

我怎样才能让它工作?

4

0 回答 0