在Meteor上使用socket.io包

时间:2022-08-22 16:36:21

We have two completely different projects. One built with nodejs and the other using Meteor. I'm handling the Meteor project and I need a certain data from the nodejs project (Which is using socket.io). Provided that I'm not allowed to modify any of the nodejs project code, I need to communicate with it using only socket.io.

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

I did install the socket.io package correctly via npm and I want to fully test first if I can connect before diving into the real coding.

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

Here's my attempt:

这是我的尝试:

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");
        }
    });
}

Upon starting up the server, I encountered this error:

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

=> 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

I can't interpret the given error. How can I fix this?

我无法解释给定的错误。我怎样才能解决这个问题?

Last thing, the connection will be 2-way. I should be sending and receiving data. How can I make this work?

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

1 个解决方案

#1


1  

It seems that you have installed the package right in your meteor project directory, i.e., you now have a node_modules directory in your meteor directory. Remember that meteor will read all subdirectories other than a few special ones (private, ..?), and will try to make the html files available as templates and load all the js files. The package you installed clearly has a lot more than the library itself, and those other files confuse meteor.

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

To solve this, just move your node_modules directory one up, or install the packages you need globally (npm install -g).

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

Also see this: How do we or can we use node modules via npm with Meteor?

另请参阅:我们如何通过npm与Meteor一起使用节点模块?

#1


1  

It seems that you have installed the package right in your meteor project directory, i.e., you now have a node_modules directory in your meteor directory. Remember that meteor will read all subdirectories other than a few special ones (private, ..?), and will try to make the html files available as templates and load all the js files. The package you installed clearly has a lot more than the library itself, and those other files confuse meteor.

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

To solve this, just move your node_modules directory one up, or install the packages you need globally (npm install -g).

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

Also see this: How do we or can we use node modules via npm with Meteor?

另请参阅:我们如何通过npm与Meteor一起使用节点模块?