UNPKG

@kevin_men/websocket

Version:

TDengine Connector for nodejs and browser using WebSocket.

72 lines (71 loc) 2.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const config_1 = require("../src/common/config"); const constant_1 = require("../src/tmq/constant"); const src_1 = require("../src"); const stable = 'meters'; const db = 'power'; const topics = ['pwer_meters_topic']; let dropTopic = `DROP TOPIC IF EXISTS ${topics[0]};`; let configMap = new Map([ [constant_1.TMQConstants.GROUP_ID, "gId"], [constant_1.TMQConstants.CONNECT_USER, "root"], [constant_1.TMQConstants.CONNECT_PASS, "taosdata"], [constant_1.TMQConstants.AUTO_OFFSET_RESET, "earliest"], [constant_1.TMQConstants.CLIENT_ID, 'test_tmq_client'], [constant_1.TMQConstants.WS_URL, 'ws://localhost:6041'], [constant_1.TMQConstants.ENABLE_AUTO_COMMIT, 'true'], [constant_1.TMQConstants.AUTO_COMMIT_INTERVAL_MS, '1000'] ]); let dsn = 'ws://root:taosdata@localhost:6041'; async function Prepare() { let conf = new config_1.WSConfig(dsn); const createDB = `create database if not exists ${db} KEEP 3650 DURATION 10 BUFFER 16 WAL_LEVEL 1;`; const createStable = `CREATE STABLE if not exists ${db}.${stable} (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int);`; let createTopic = `create topic if not exists ${topics[0]} as select * from ${db}.${stable}`; const useDB = `use ${db}`; let ws = await (0, src_1.sqlConnect)(conf); await ws.exec(createDB); await ws.exec(useDB); await ws.exec(createStable); await ws.exec(createTopic); for (let i = 0; i < 10; i++) { await ws.exec(`INSERT INTO d1001 USING ${stable} (location, groupId) TAGS ("California.SanFrancisco", 3) VALUES (NOW, ${10 + i}, ${200 + i}, ${0.32 + i})`); } ws.close(); } (async () => { let consumer = null; try { await Prepare(); consumer = await (0, src_1.tmqConnect)(configMap); await consumer.subscribe(topics); for (let i = 0; i < 5; i++) { let res = await consumer.poll(500); for (let [key, value] of res) { console.log(key, value); } if (res.size == 0) { break; } await consumer.commit(); } let assignment = await consumer.assignment(); console.log(assignment); await consumer.seekToBeginning(assignment); assignment = await consumer.assignment(); for (let i in assignment) { console.log("seek after:", assignment[i]); } await consumer.unsubscribe(); } catch (e) { console.error(e); } finally { if (consumer) { await consumer.close(); } (0, src_1.destroy)(); } })();