在流星上使用socket.io软件包



我们有两个完全不同的项目。一个用nodejs构建,另一个使用流星构建。我正在处理流星项目,我需要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

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

最后一件事,连接将是2路。我应该发送和接收数据。我该如何使这项工作?

看来,您已经在流星项目目录中安装了软件包,即,您现在在流星目录中有一个node_modules目录。请记住,流星将读取 All 子目录以外的其他副业(私有,..?),并且会尝试使HTML文件作为模板可用并加载所有JS文件。您安装的软件包显然比库本身要多得多,而其他文件使流星感到困惑。

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

还看到以下内容:我们如何或可以通过Meteor通过NPM使用节点模块?

最新更新