NodeJS API – 客户端的“networking”

我想在客户端使用api – “net”( https://nodejs.org/api/net.html )。 我的代码看起来像这样:

const net = require('net'); const client = net.connect({port: 8124}, () => { console.log('connected to server!'); client.write('hello world!'); }); client.on('end', () => { console.log('disconnected from server'); }); 

我不想使用websocket,socketio或其他库。 任何想法,我怎么能得到这个工作?

非常感谢!

Net在浏览器上不可用,因为浏览器无法打开任意的TCP套接字。 根据你的情况,几乎没有什么可供select的选项是net-browserifyhttp-stream