как запустить программу при нажатии кнопки запуска в веб-приложении - PullRequest
0 голосов
/ 25 апреля 2019

Я хочу создать симуляцию, которая всегда отправляет сообщения.Код прост.Но проблема в том, что я не знаю, как запустить его, когда я нажму на старт.Он запускается только когда я нажимаю URL.Я использую nodejs.

enter image description here

Кстати, мой код такой, как показано ниже

// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

'use strict';

// Connection string for the IoT Hub service
//
// NOTE:
// For simplicity, this sample sets the connection string in code.
// In a production environment, the recommended approach is to use
// an environment variable to make it available to your application
// or use an x509 certificate.
// https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-security
//
// Using the Azure CLI:
// az iot hub show-connection-string --hub-name {YourIoTHubName} --output table
var connectionString = 'xxxxxxx';

// Using the Node.js SDK for Azure Event hubs:
//   https://github.com/Azure/azure-event-hubs-node
// The sample connects to an IoT hub's Event Hubs-compatible endpoint
// to read messages sent from a device.
var { EventHubClient, EventPosition } = require('@azure/event-hubs');

var printError = function (err) {
  console.log(err.message);
};

// Display the message content - telemetry and properties.
// - Telemetry is sent in the message body
// - The device can add arbitrary application properties to the message
// - IoT Hub adds system properties, such as Device Id, to the message.
var printMessage = function (message) {
  console.log('Telemetry received: ');
  console.log(JSON.stringify(message.body));
  console.log('Application properties (set by device): ')
  console.log(JSON.stringify(message.applicationProperties));
  console.log('System properties (set by IoT Hub): ')
  console.log(JSON.stringify(message.annotations));
  console.log('');
};

// Connect to the partitions on the IoT Hub's Event Hubs-compatible endpoint.
// This example only reads messages sent after this application started.
var ehClient;
EventHubClient.createFromIotHubConnectionString(connectionString).then(function (client) {
  console.log("Successully created the EventHub Client from iothub connection string.");
  ehClient = client;
  return ehClient.getPartitionIds();
}).then(function (ids) {
  console.log("The partition ids are: ", ids);
  return ids.map(function (id) {
    return ehClient.receive(id, printMessage, printError, { eventPosition: EventPosition.fromEnqueuedTime(Date.now()) });
  });
}).catch(printError);

1 Ответ

1 голос
/ 25 апреля 2019

Полагаю, вы хотите, чтобы ваше приложение работало в фоновом режиме.Таким образом, вы можете развернуть свой файл nodejs в качестве веб-задания.Полная документация о веб-заданиях Azure здесь: Запуск фоновых задач с веб-заданиями в службе приложений Azure .

Это блог о веб-заданиях с Node.js.Вам нужно создать файл run.js, а затем заархивировать файл run.js со всеми зависимостями (включая каталог node_modules).Затем загрузите почтовый индекс в качестве веб-задания.А по вашему требованию веб-работа должна быть непрерывной.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...