var net = require('net');
var client = new net.Socket();
client.connect(port, 'ip', function() {
console.log('Connected');
client.write('meesage');
});
client.on('data', function(data) {
client.write('meesage');
console.log('Received: ' + data);
client.destroy();
});
client.on('close', function() {
console.log('Connection closed');
});
我尝试在浏览器中使用浏览器而不是访问,我想将消息tcp客户端发送到一个有网络模块的设备上。
发布于 2022-11-16 09:54:54
不能在浏览器中运行这种类型的nodejs代码。这依赖于没有在浏览器中运行的net
库,它只在nodejs环境中运行。Browserify帮助在浏览器中运行环境之间的端口,但不提供浏览器中不存在的本机网络功能或文件系统访问(或其他类似类型的函数)。因此,您只能在使用浏览器支持的功能的网页中运行代码。
浏览器允许网页中的Javascript发出http请求或连接到webSocket服务器,或者使用webRTC进行网络连接。您不能通过浏览器Javascript创建自己的低级别TCP或UDP套接字。
https://stackoverflow.com/questions/74457685
复制相似问题