最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sending UDP packets in Electron through Node.js datagram - Stack Overflow

programmeradmin1浏览0评论

I'm trying to send a udp packet using the node.js dgram package from an electron app. The send method calls for a Buffer, Uint8Array, or string. I'm getting a type error when I try to use a Buffer or UintArray however. The exact error is: TypeError: First argument must be a buffer or a string.

I can see in the Node.js dgram code that it is checking for a buffer using !(buffer instanceof Buffer . If I try using instanceof on the buffer in my code it just returns object as the type, while if I try 'Buffer.isBuffer()' it returns true. I've tried all the methods that node provides for creating buffers buffer with no results. Send does accept strings but then I am running into all sorts of issues with encoding, and that feels kinda of hacky.

It looks like electron is running node 7.4.0.

I see my options from here as:

  1. Edit the node.js code
  2. Somehow override what instanceof returns for my buffer object
  3. Figure out a solution for properly encoding the Buffer as a string.

Any ideas?

I'm trying to send a udp packet using the node.js dgram package from an electron app. The send method calls for a Buffer, Uint8Array, or string. I'm getting a type error when I try to use a Buffer or UintArray however. The exact error is: TypeError: First argument must be a buffer or a string.

I can see in the Node.js dgram code that it is checking for a buffer using !(buffer instanceof Buffer . If I try using instanceof on the buffer in my code it just returns object as the type, while if I try 'Buffer.isBuffer()' it returns true. I've tried all the methods that node provides for creating buffers buffer with no results. Send does accept strings but then I am running into all sorts of issues with encoding, and that feels kinda of hacky.

It looks like electron is running node 7.4.0.

I see my options from here as:

  1. Edit the node.js code
  2. Somehow override what instanceof returns for my buffer object
  3. Figure out a solution for properly encoding the Buffer as a string.

Any ideas?

Share Improve this question asked Jun 19, 2017 at 23:26 Russill GloverRussill Glover 711 silver badge6 bronze badges 2
  • Did you use Buffer.isBuffer(), like on the class Buffer or on your actual instance? Have you also tried using Buffer.from() to create your buffer? – RoyalBingBong Commented Jun 20, 2017 at 9:44
  • I've tried using Buffer.from and new Buffer(). Also I was mistaken, if I try instanceof Buffer on my buffer it is returning true. I think the issue may be with the Node dgram module relying on a different version of Buffer. Is there anyway I can change the node code with repiling? – Russill Glover Commented Jun 20, 2017 at 16:47
Add a ment  | 

1 Answer 1

Reset to default 5

To get this to work I just had to point my global Buffer value to the node buffer module as so: const Buffer = window.require('buffer').Buffer

发布评论

评论列表(0)

  1. 暂无评论