在Meteor上使用socket.io包

时间:2014-01-29 17:15:22

标签: node.js meteor

我们有两个完全不同的项目。一个使用nodejs构建,另一个使用Meteor。我正在处理Meteor项目,我需要来自nodejs项目的某些数据(使用socket.io)。如果我不允许修改任何nodejs项目代码,我只需要使用socket.io与它通信。

我确实通过npm正确安装了socket.io包,如果我能够连接到真正的编码之前我想先完全测试。

这是我的尝试:

var Websocket = Npm.require('socket.io');
var socket = Websocket.connect('http://10.10.10.10:80'); // Sample IP

if (Meteor.isClient) {

    socket.on('message', function(res){
        console.log(res);
    });

    Template.hello.greeting = function () {
        return "Test";
    };

    Template.hello.events({
        'click input' : function () {
            // template data, if any, is available in 'this'
            if (typeof console !== 'undefined')
                console.log("You pressed the button");
        }
    });
}

启动服务器后,我遇到了这个错误:

=> Your application has errors. Waiting for file change.
=> Modified -- restarting.
=> Errors prevented startup:

While building the application:
node_modules/socket.io/node_modules/socket.io-client/lib/vendor/web-socket-js/sample.html:5: bad f                                    ormatting in HTML template
node_modules/socket.io/node_modules/socket.io-client/node_modules/ws/examples/fileapi/public/index                                    .html:1: Can't set DOCTYPE here.  (Meteor sets <!DOCTYPE html> for you)
node_modules/socket.io/node_modules/socket.io-client/node_modules/ws/examples/serverstats-express_                                    3/public/index.html:1: Can't set DOCTYPE here.  (Meteor sets <!DOCTYPE html> for you)
node_modules/socket.io/node_modules/socket.io-client/node_modules/ws/examples/serverstats/public/i                                    ndex.html:1: Can't set DOCTYPE here.  (Meteor sets <!DOCTYPE html> for you)
node_modules/socket.io/node_modules/socket.io-client/node_modules/uglify-js/README.html:1: bad for                                    matting in HTML template
node_modules/socket.io/node_modules/policyfile/doc/index.html:1: bad formatting in HTML template
node_modules/socket.io/node_modules/socket.io-client/node_modules/uglify-js/test/beautify.js:1:15:                                     Unexpected token ILLEGAL
node_modules/socket.io/node_modules/socket.io-client/node_modules/uglify-js/test/testparser.js:1:1                                    5: Unexpected token ILLEGAL
node_modules/socket.io/node_modules/socket.io-client/node_modules/uglify-js/tmp/embed-tokens.js:1:                                    15: Unexpected token ILLEGAL
node_modules/socket.io/node_modules/socket.io-client/node_modules/uglify-js/tmp/test.js:1:15: Unex                                    pected token ILLEGAL
node_modules/socket.io/node_modules/redis/diff_multi_bench_output.js:1:15: Unexpected token ILLEGA                                    L

我无法解释给定的错误。我该如何解决这个问题?

最后,连接将是双向的。我应该发送和接收数据。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

您似乎已将软件包安装在meteor项目目录中,即您现在在meteor目录中有一个node_modules目录。请记住,meteor将读取所有子目录,而不是几个特殊的子目录(私有,...?),并将尝试将html文件作为模板提供并加载所有js文件。您安装的软件包明显比软件库本身要多得多,而其他文件则混淆了流星。

要解决此问题,只需将node_modules目录移动一个,或者全局安装所需的软件包(npm install -g)。

另见:How do we or can we use node modules via npm with Meteor?