From d4b79eea8e59f7cf5fb49df567c1d6515076a821 Mon Sep 17 00:00:00 2001 From: Anton Palgunov Date: Thu, 27 Feb 2020 21:59:50 +0000 Subject: [PATCH] added closeConnextion --- README.md | 5 +++++ example/index-dev.js | 1 + src/index.ts | 7 +++++-- src/samsung.ts | 25 ++++++++++++++++++++++--- tsconfig.json | 2 +- tslint.json | 2 +- 6 files changed, 35 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 5272f18..a5010f7 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ Also you can use the lib in your Node-RED https://github.com/Toxblh/node-red-con You can try [example code](example/index.js) ```js +// import Samsung, { APPS, KEYS } from 'samsung-tv-control' const { Samsung, KEYS, APPS } = require('samsung-tv-control') const config = { @@ -73,6 +74,10 @@ control console.log('# Response openApp', res) } }) + + // Control will keep connection for next messages in 1 minute + // If you would like to close it immediately, you can use `closeConnection()` + control.closeConnection() }) .catch(e => console.error(e)) ``` diff --git a/example/index-dev.js b/example/index-dev.js index 906518b..5312796 100644 --- a/example/index-dev.js +++ b/example/index-dev.js @@ -38,6 +38,7 @@ async function main() { // await control.openApp(APPS.YouTube) await control.sendKeyPromise(KEYS.KEY_POWER) await control.getLogs() + control.closeConnection() return } diff --git a/src/index.ts b/src/index.ts index ef8c46e..2aa54d8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,6 @@ +import Samsung from './samsung' + export { default as Samsung } from './samsung' -export { default as KEYS } from './keys'; -export { default as APPS } from './apps'; +export { default as KEYS } from './keys' +export { default as APPS } from './apps' +export default Samsung diff --git a/src/samsung.ts b/src/samsung.ts index 5f7882f..33f27f2 100644 --- a/src/samsung.ts +++ b/src/samsung.ts @@ -8,7 +8,15 @@ import * as WebSocket from 'ws' import { KEYS } from './keys' import Logger from './logger' import { Configuration, WSData, App, Command } from './types' -import { base64, chr, getVideoId, getMsgInstalledApp, getMsgLaunchApp, getCommandByKey, getSendTextCommand } from './helpers' +import { + base64, + chr, + getVideoId, + getMsgInstalledApp, + getMsgLaunchApp, + getCommandByKey, + getSendTextCommand +} from './helpers' class Samsung { private IP: string @@ -230,7 +238,11 @@ class Samsung { }, (err, response) => { if (!err) { - this.LOGGER.log('Link sended', { status: response.statusCode, body: response.body, headers: response.headers }, 'openYouTubeLink') + this.LOGGER.log( + 'Link sended', + { status: response.statusCode, body: response.body, headers: response.headers }, + 'openYouTubeLink' + ) resolve('Link sended') } else { this.LOGGER.error('While send a link, somthing went wrong', { err }, 'openYouTubeLink') @@ -294,6 +306,13 @@ class Samsung { this.LOGGER.saveLogToFile() } + /** + * If you don't need to keep connection, you can to close immediately + */ + public closeConnection() { + this.wsClose() + } + private _send( command: Command, done?: (err: null | (Error & { code: string }), res: WSData | null) => void, @@ -514,4 +533,4 @@ class Samsung { } } -export default Samsung \ No newline at end of file +export default Samsung diff --git a/tsconfig.json b/tsconfig.json index c4dcf3b..660835e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,5 +13,5 @@ "skipLibCheck": true }, "include": ["src/**/*"], - "exclude": ["src/**/*.spec.*"] + "exclude": ["src/**/*.spec.*", "**/*.spec.ts"] } diff --git a/tslint.json b/tslint.json index 4321563..aafe736 100644 --- a/tslint.json +++ b/tslint.json @@ -10,6 +10,6 @@ }, "rulesDirectory": [], "linterOptions": { - "exclude": ["node_modules/**/*.{ts,tsx}", "**/*.{js,jsx,mjs,json}"] + "exclude": ["node_modules/**/*.{ts,tsx}", "**/*.{js,jsx,mjs,json}", "src/**/*.spec.*"] } }