1

我想用 Visual Studio 代码制作一个简单的客户端服务器示例。对于我的 mqtt 客户端实例,mosca 不起作用。所以我用伊蚊创建了一个服务器。但是,目前无法连接到 client.js。我确定它在服务器端丢失,但我不确定如何修复它。我对此很陌生。我的代码如下。

服务器;

const aedes = require('aedes')()
const server = require('net').createServer(aedes.handle)
const httpServer = require('http').createServer()
const ws = require('websocket-stream')
const port = 1883
const wsPort = 3000

server.listen(port, function () {
  console.log('server started and listening on port ', port)
})

ws.createServer({ server: httpServer }, aedes.handle)

httpServer.listen(wsPort, function () {
  console.log('websocket server listening on port ', wsPort)
})

客户;

var mqtt = require('mqtt');
var client = mqtt.connect('mqtt://192.168.43.40:1883');

client.subscribe('new-user');

client.on('connect', function() {
    console.log('connected!');

    client.publish('new-user', 'Cansu-' + Math.ceil(Math.random() * 10));
});

client.on('message', function(topic, message) {
    console.log(topic, ' : ', message.toString());
    client.end();
});

谢谢你!!!

4

0 回答 0