I am trying to use simple-peer package as below:
我正在尝试使用简单对等包,如下所示:
var Peer = require('simple-peer');
var peer = new Peer({});
peer.on('signal', function(data) {
console.log(JSON.stringify(data));
});
I get nothing printed to the console and the below error appear:
我没有得到任何打印到控制台和下面的错误出现:
SCRIPT5009: 'Buffer' is undefined
SCRIPT5009:“缓冲”是未定义的
NOTE: My project is chat application that works fine when sending text messages, but after I add the above code the chat doesn't work as well.
注意:我的项目是聊天应用程序,在发送文本消息时运行良好,但是在添加上述代码之后,聊天就不能正常工作了。
I am using other npm packages such as socket.io and they are working fine.
我正在使用其他的npm包,比如socket。爱娥和他们工作得很好。
What am I doing wrong here?
我在这里做错了什么?
2 个解决方案
#1
2
Because Buffer is a Node global for server-side use, you need to include a polyfill to use the same library on the client.
因为Buffer是服务器端全局节点,所以需要包含一个polyfill,以便在客户机上使用相同的库。
Meteor makes this easy with the meteor-node-stubs
package. Install it with:
流星使这容易与流星-节点-存根包。安装:
meteor npm install --save meteor-node-stubs
Note that these are not small and may significantly increase your bundle size. Keep an eye out for how much impact it has so you can decide if the use of this particular package is worth the added weight.
注意,这些并不小,可能会显著增加您的bundle大小。留意它有多大的影响,这样你就可以决定使用这个特别的包是否值得增加重量。
To make sure it's available to the module that's expecting it, you might need to add it to the window
before initializing simple-peer
:
为了确保它对期望它的模块可用,您可能需要在初始化simple-peer之前将它添加到窗口中:
window.Buffer = require('buffer').Buffer
Add this to your main js file (or whichever is the first to run)
将其添加到主js文件中(或以第一个运行的文件为准)
#2
1
According to the nodejs buffer documentation...
根据nodejs缓冲区文档……
The Buffer class is a global within Node.js, making it unlikely that one would need to ever use require('buffer').Buffer.
缓冲区类是节点内的全局类。js不太可能需要使用require('buffer'). buffer。
but you might need to add a line
但是您可能需要添加一行
var Buffer = require('buffer').Buffer
To make it available to this module. Meteor is not on the most current version of node - upgrading to 1.6 may also solve the problem.
以使它可用到这个模块。流星并不是目前最流行的节点——升级到1.6也可能解决问题。
#1
2
Because Buffer is a Node global for server-side use, you need to include a polyfill to use the same library on the client.
因为Buffer是服务器端全局节点,所以需要包含一个polyfill,以便在客户机上使用相同的库。
Meteor makes this easy with the meteor-node-stubs
package. Install it with:
流星使这容易与流星-节点-存根包。安装:
meteor npm install --save meteor-node-stubs
Note that these are not small and may significantly increase your bundle size. Keep an eye out for how much impact it has so you can decide if the use of this particular package is worth the added weight.
注意,这些并不小,可能会显著增加您的bundle大小。留意它有多大的影响,这样你就可以决定使用这个特别的包是否值得增加重量。
To make sure it's available to the module that's expecting it, you might need to add it to the window
before initializing simple-peer
:
为了确保它对期望它的模块可用,您可能需要在初始化simple-peer之前将它添加到窗口中:
window.Buffer = require('buffer').Buffer
Add this to your main js file (or whichever is the first to run)
将其添加到主js文件中(或以第一个运行的文件为准)
#2
1
According to the nodejs buffer documentation...
根据nodejs缓冲区文档……
The Buffer class is a global within Node.js, making it unlikely that one would need to ever use require('buffer').Buffer.
缓冲区类是节点内的全局类。js不太可能需要使用require('buffer'). buffer。
but you might need to add a line
但是您可能需要添加一行
var Buffer = require('buffer').Buffer
To make it available to this module. Meteor is not on the most current version of node - upgrading to 1.6 may also solve the problem.
以使它可用到这个模块。流星并不是目前最流行的节点——升级到1.6也可能解决问题。