2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Node.js: 一定時間毎に udp 送信を行う

Last updated at Posted at 2021-08-30

こちらにある udpclient.js を改造しました。
Telegraf の入力データフォーマットを JSON にする

udp_plural.js
#! /usr/bin/node
// ---------------------------------------------------------------
//
//	udp_plural.js
//
//					Aug/30/2021	
// ---------------------------------------------------------------
'use strict'

// UDP 接続先
const host = "localhost"
const c_port = 8092
var dgram = require("dgram")
var client = dgram.createSocket("udp4")

// ---------------------------------------------------------------
// サーバからメッセージ受信したときの処理
client.on("message", function(msg, rinfo) {
	console.log("recieved: " + msg.toString("hex"))
	client.close()
})

// ---------------------------------------------------------------
// メッセージ送信でエラーが起きた時の処理
client.on("err", function(err) {
	console.log("client error: \n" + err.stack);
	console.close()
})

// ---------------------------------------------------------------
// Socket をクローズした時の処理
client.on("close", function() {
	console.log("closed.")
})

// ---------------------------------------------------------------
// メッセージ送信
function send(message, host, port)
{
	client.send(message, 0, message.length, port, host, function(err, bytes)
	{
	console.log("*** sent ***")
	client.close()
	})
}

// ---------------------------------------------------------------
async function send_proc(temperature,humidity)
{
	client = dgram.createSocket("udp4")

	var unit_aa = new Object ()
	unit_aa["name"] = "aaa"
	unit_aa["temperature"] = temperature
	unit_aa["humidity"] = humidity
	const json_str = JSON.stringify(unit_aa)
	console.log(json_str)

	const message = new Buffer.from(json_str)
	send(message, host, c_port)
}

// ---------------------------------------------------------------
async function s02(temperature,humidity)
{
	await send_proc(temperature,humidity)

	await new Promise(resolve => setTimeout(resolve, 10000))
}

// ---------------------------------------------------------------
async function main()
{
	console.error ("*** 開始 ***")

	await s02(25.2,50.5)
	await s02(25.9,60.7)
	await s02(26.8,63.4)
	await s02(27.2,70.8)
	await s02(26.5,65.6)
	await s02(26.0,55.3)
	await s02(25.5,51.2)

	console.error ("*** 終了 ***")
}

// ---------------------------------------------------------------
main()

// ---------------------------------------------------------------

実行結果

$ ./udp_plural.js 
*** 開始 ***
{"name":"aaa","temperature":25.2,"humidity":50.5}
*** sent ***
{"name":"aaa","temperature":25.9,"humidity":60.7}
*** sent ***
{"name":"aaa","temperature":26.8,"humidity":63.4}
*** sent ***
{"name":"aaa","temperature":27.2,"humidity":70.8}
*** sent ***
{"name":"aaa","temperature":26.5,"humidity":65.6}
*** sent ***
{"name":"aaa","temperature":26,"humidity":55.3}
*** sent ***
{"name":"aaa","temperature":25.5,"humidity":51.2}
*** sent ***
*** 終了 ***

次のバージョンで確認しました。

$ node --version
v14.17.5

こちらのページを参考にしました。
ES2017のasync/awaitを使って「同期的sleep処理」をワンライナーで書く

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?