服务器未获取客户端发出的消息-NestJS MQTT
我是Nest的新手,我正在尝试集成MQTT协议。一切正常,只是服务器没有从服务器上实例化的客户端收到任何消息服务器未获取客户端发出的消息-NestJS MQTT,nestjs,Nestjs,我是Nest的新手,我正在尝试集成MQTT协议。一切正常,只是服务器没有从服务器上实例化的客户端收到任何消息 main.ts async function bootstrap() { // Create regular nest application. const app = await NestFactory.create(AppModule); // Then combine it with your microservice const mqtt = app.conne
main.ts
async function bootstrap() {
// Create regular nest application.
const app = await NestFactory.create(AppModule);
// Then combine it with your microservice
const mqtt = app.connectMicroservice({
transport: Transport.MQTT,
options: {
url: 'brokerUrl',
clientId: 'id',
username: 'username',
password: 'password'
}
});
await app.startAllMicroservicesAsync();
await app.listen(3000);
}
bootstrap();
@Module({
imports: [
ClientsModule.register([
{
name: 'MQ_CLIENT',
transport: Transport.MQTT,
options: {
url: 'brokerUrl',
clientId: 'id',
username: 'username',
password: 'password'
}
},
]),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
@Controller()
export class AppController {
constructor(private appService: AppService) {}
@EventPattern('test')
getNotifications(data) {
console.log(data); // nothing is received
}
@Get('/')
test() {
// send a message when accessing route /
this.appService.sendMessage();
}
}
@Injectable()
export class AppService implements OnModuleInit {
constructor(@Inject('MQ_CLIENT') private client: ClientProxy){
}
async onModuleInit() {
console.log('connecting');
await this.client.connect();
console.log('connected');
}
sendMessage() {
this.client.emit('test', 'some data');
}
}
AppModule.ts
async function bootstrap() {
// Create regular nest application.
const app = await NestFactory.create(AppModule);
// Then combine it with your microservice
const mqtt = app.connectMicroservice({
transport: Transport.MQTT,
options: {
url: 'brokerUrl',
clientId: 'id',
username: 'username',
password: 'password'
}
});
await app.startAllMicroservicesAsync();
await app.listen(3000);
}
bootstrap();
@Module({
imports: [
ClientsModule.register([
{
name: 'MQ_CLIENT',
transport: Transport.MQTT,
options: {
url: 'brokerUrl',
clientId: 'id',
username: 'username',
password: 'password'
}
},
]),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
@Controller()
export class AppController {
constructor(private appService: AppService) {}
@EventPattern('test')
getNotifications(data) {
console.log(data); // nothing is received
}
@Get('/')
test() {
// send a message when accessing route /
this.appService.sendMessage();
}
}
@Injectable()
export class AppService implements OnModuleInit {
constructor(@Inject('MQ_CLIENT') private client: ClientProxy){
}
async onModuleInit() {
console.log('connecting');
await this.client.connect();
console.log('connected');
}
sendMessage() {
this.client.emit('test', 'some data');
}
}
AppController.ts
async function bootstrap() {
// Create regular nest application.
const app = await NestFactory.create(AppModule);
// Then combine it with your microservice
const mqtt = app.connectMicroservice({
transport: Transport.MQTT,
options: {
url: 'brokerUrl',
clientId: 'id',
username: 'username',
password: 'password'
}
});
await app.startAllMicroservicesAsync();
await app.listen(3000);
}
bootstrap();
@Module({
imports: [
ClientsModule.register([
{
name: 'MQ_CLIENT',
transport: Transport.MQTT,
options: {
url: 'brokerUrl',
clientId: 'id',
username: 'username',
password: 'password'
}
},
]),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
@Controller()
export class AppController {
constructor(private appService: AppService) {}
@EventPattern('test')
getNotifications(data) {
console.log(data); // nothing is received
}
@Get('/')
test() {
// send a message when accessing route /
this.appService.sendMessage();
}
}
@Injectable()
export class AppService implements OnModuleInit {
constructor(@Inject('MQ_CLIENT') private client: ClientProxy){
}
async onModuleInit() {
console.log('connecting');
await this.client.connect();
console.log('connected');
}
sendMessage() {
this.client.emit('test', 'some data');
}
}
AppService.ts
async function bootstrap() {
// Create regular nest application.
const app = await NestFactory.create(AppModule);
// Then combine it with your microservice
const mqtt = app.connectMicroservice({
transport: Transport.MQTT,
options: {
url: 'brokerUrl',
clientId: 'id',
username: 'username',
password: 'password'
}
});
await app.startAllMicroservicesAsync();
await app.listen(3000);
}
bootstrap();
@Module({
imports: [
ClientsModule.register([
{
name: 'MQ_CLIENT',
transport: Transport.MQTT,
options: {
url: 'brokerUrl',
clientId: 'id',
username: 'username',
password: 'password'
}
},
]),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
@Controller()
export class AppController {
constructor(private appService: AppService) {}
@EventPattern('test')
getNotifications(data) {
console.log(data); // nothing is received
}
@Get('/')
test() {
// send a message when accessing route /
this.appService.sendMessage();
}
}
@Injectable()
export class AppService implements OnModuleInit {
constructor(@Inject('MQ_CLIENT') private client: ClientProxy){
}
async onModuleInit() {
console.log('connecting');
await this.client.connect();
console.log('connected');
}
sendMessage() {
this.client.emit('test', 'some data');
}
}
消息已正确发送到代理,但在应订阅的嵌套服务器中未接收到消息。如果消息是从其他客户端(嵌套之外)发送的,则会收到这些消息。您使用哪台服务器发送消息?兔子?卡夫卡?我在用摩斯基托