Last active
December 7, 2020 14:06
-
-
Save serhatates/d3dc5aad86b9cbb524b24c1d741219f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const net = require('net'); | |
const clientTcp = net.Socket(); | |
let intervalTcpConnect = false; | |
var isTcpConnected = false; | |
function connectTcp() { | |
clientTcp.connect({ | |
host: 'localhost', | |
port: 5060, | |
}); | |
} | |
function launchTcpIntervalConnect() { | |
if (intervalTcpConnect != false) return; | |
isTcpConnected = false; | |
intervalTcpConnect = setInterval(connectTcp, 3 * 1000); | |
} | |
function clearTcpIntervalConnect() { | |
if (intervalTcpConnect == false) return; | |
clearInterval(intervalTcpConnect); | |
intervalTcpConnect = false; | |
} | |
clientTcp.on('connect', () => { | |
isTcpConnected = true; | |
clearTcpIntervalConnect(); | |
console.log('connected to %s:%s', 'localhost', 5060); | |
clientTcp.write('client connected'); | |
}); | |
clientTcp.on('error', (err) => { | |
console.error(err.message); | |
launchTcpIntervalConnect(); | |
}); | |
clientTcp.on('close', launchTcpIntervalConnect); | |
clientTcp.on('end', launchTcpIntervalConnect); | |
connectTcp(); | |
function sendTcpMessageTest(msg) { | |
if (isTcpConnected) clientTcp.write(msg); | |
} | |
setInterval(() => { | |
sendTcpMessageTest('sendTcpMessageTest()'); | |
}, 3 * 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment