dgram#createSocket TypeScript Examples
The following examples show how to use
dgram#createSocket.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.spec.ts From hoprnet with GNU General Public License v3.0 | 7 votes |
/**
* Creates a UDP socket and binds it to the given port.
* @param port port to which the socket should be bound
* @returns a bound socket
*/
export function bindToUdpSocket(port?: number): Promise<Socket> {
const socket = createSocket('udp4')
return new Promise<Socket>((resolve, reject) => {
socket.once('error', (err: any) => {
socket.removeListener('listening', resolve)
reject(err)
})
socket.once('listening', () => {
socket.removeListener('error', reject)
resolve(socket)
})
try {
socket.bind(port)
} catch (err) {
reject(err)
}
})
}
Example #2
Source File: mavesp.ts From node-mavlink with GNU Lesser General Public License v3.0 | 6 votes |
/**
* Start communication with the controller via MAVESP2866
*
* @param receivePort port to receive messages on (default: 14550)
* @param sendPort port to send messages to (default: 14555)
*/
async start(receivePort: number = 14550, sendPort: number = 14555) {
this.sendPort = sendPort
// Create a UDP socket
this.socket = createSocket({ type: 'udp4', reuseAddr: true })
this.socket.on('message', this.processIncommingUDPData)
// Start listening on the socket
return new Promise((resolve, reject) => {
this.socket.bind(receivePort, () => {
// Wait for the first package to be returned to read the ip address
// of the controller
waitFor(() => this.ip !== '')
.then(() => { resolve(this.ip) })
.catch(e => { reject(e) })
})
})
}
Example #3
Source File: session.ts From eufy-security-client with MIT License | 6 votes |
private onClose(): void {
this.socket.removeAllListeners();
this.socket = createSocket("udp4");
this.socket.on("message", (msg, rinfo) => this.handleMsg(msg, rinfo));
this.socket.on("error", (error) => this.onError(error));
this.socket.on("close", () => this.onClose());
this.binded = false;
this._disconnected();
}
Example #4
Source File: session.ts From eufy-security-client with MIT License | 6 votes |
constructor(rawStation: StationListResponse, api: HTTPApi) {
super();
this.api = api;
this.log = api.getLog();
this.updateRawStation(rawStation);
this.socket = createSocket("udp4");
this.socket.on("message", (msg, rinfo) => this.handleMsg(msg, rinfo));
this.socket.on("error", (error) => this.onError(error));
this.socket.on("close", () => this.onClose());
this._initialize();
}
Example #5
Source File: rtp.ts From homebridge-nest-cam with GNU General Public License v3.0 | 5 votes |
public readonly socket = createSocket('udp4');
Example #6
Source File: getRoombas.ts From homebridge-iRobot with Apache License 2.0 | 5 votes |
function getDeviceCredentials(blid: string, ip?: string): Promise<ConfiguredRoomba> {
return new Promise((resolve, reject) => {
let broadcastInterval;
const server = createSocket('udp4');
let devices = false;
server.on('error', (err) => {
reject(err);
//console.error(err);
});
server.on('message', (msg) => {
devices = true;
try {
const parsedMsg = JSON.parse(msg.toString());
if (parsedMsg.hostname && parsedMsg.ip &&
((parsedMsg.hostname.split('-')[0] === 'Roomba') || (parsedMsg.hostname.split('-')[0] === 'iRobot'))) {
if (parsedMsg.hostname.split('-')[1] === blid) {
clearInterval(broadcastInterval);
server.close();
let Major;
parsedMsg.sw.split('').forEach((version) => {
if (Major === undefined && version === '1') {
Major = 1;
} else if (Major === undefined && version === '2') {
Major = 2;
} else if (Major === undefined && version === '3') {
Major = 3;
} else if (Major) {
parsedMsg.swMajor = Major;
resolve(parsedMsg);
}
});
//parsedMsg.swMajor = 2;
//resolve(parsedMsg);
//console.log(JSON.stringify(parsedMsg));
//process.exit(0);
}
}
} catch (e) {
reject(e);
}
});
/*
server.on('listening', () => {
setTimeout(()=>{
//console.log(child_process.execFileSync(__dirname + '/getRoombaIP.js', [blid, attempt+1]).toString());
//process.exit(0);
}, 5000);
});
*/
server.bind(() => {
const message = Buffer.from('irobotmcs');
server.setBroadcast(ip === undefined);
server.send(message, 0, message.length, 5678, ip || '255.255.255.255');
let attempts = 0;
broadcastInterval = setInterval(() => {
attempts++;
if (attempts > 5) {
if (!devices) {
reject('UDP Disabled');
} else {
reject('No Roomba Found');
}
clearInterval(broadcastInterval);
} else {
server.send(message, 0, message.length, 5678, ip || '255.255.255.255');
}
}, 5000);
});
});
}
Example #7
Source File: listener.ts From hoprnet with GNU General Public License v3.0 | 5 votes |
/**
* @param onClose called once listener is closed
* @param onListening called once listener is listening
* @param dialDirectly utility to establish a direct connection
* @param upgradeInbound forward inbound connections to libp2p
* @param peerId own id
* @param options connection Options, e.g. AbortSignal
* @param testingOptions turn on / off modules for testing
* @param filter allow Listener to populate address filter
* @param relay allow Listener to populate list of utilized relays
* @param libp2p libp2p instance for various purposes
*/
constructor(
private onClose: () => void,
private onListening: () => void,
dialDirectly: HoprConnect['dialDirectly'],
private upgradeInbound: Upgrader['upgradeInbound'],
private peerId: PeerId,
private options: HoprConnectOptions,
private testingOptions: HoprConnectTestingOptions,
private filter: Filter,
private relay: Relay,
libp2p: Libp2p
) {
super()
this.__connections = []
this.tcpSocket = createServer()
this.udpSocket = createSocket({
// @TODO
// `udp6` does not seem to work in Node 12.x
// can receive IPv6 packet and IPv4 after reconnecting the socket
type: 'udp4',
// set to true to reuse port that is bound
// to TCP socket
reuseAddr: true
})
this.state = State.UNINITIALIZED
this.addrs = {
interface: [],
external: []
}
this._emitListening = function (this: Listener) {
// hopr-connect does not enable IPv6 connections right now, therefore we can set `listeningAddrs` statically
// to `/ip4/0.0.0.0/tcp/0`, meaning listening on IPv4 using a canonical port
// TODO check IPv6
this.filter.setAddrs(this.getAddrs(), [new Multiaddr(`/ip4/0.0.0.0/tcp/0/p2p/${this.peerId.toB58String()}`)])
const usedRelays = this.entry.getUsedRelayAddresses()
if (usedRelays && usedRelays.length > 0) {
const relayPeerIds = this.entry.getUsedRelayAddresses().map((ma: Multiaddr) => {
const tuples = ma.tuples()
return PeerId.createFromBytes((tuples[0][1] as Uint8Array).slice(1))
})
this.relay.setUsedRelays(relayPeerIds)
}
this.emit('listening')
}.bind(this)
this.entry = new EntryNodes(this.peerId, libp2p, dialDirectly, this.options)
this.upnpManager = new UpnpManager()
}
Example #8
Source File: new-streaming-delegate.ts From homebridge-plugin-eufy-security with Apache License 2.0 | 4 votes |
private startStream(
request: StartStreamRequest,
callback: StreamRequestCallback,
): void {
this.platform.httpService
.startStream({
device_sn: this.device.device_sn,
station_sn: this.device.station_sn,
proto: 2,
})
.then(async ({ url }) => {
await new Promise((r) => setTimeout(r, 500));
return url;
})
.then((url) => {
const sessionInfo = this.pendingSessions[request.sessionID];
const vcodec = 'libx264';
const mtu = 1316; // request.video.mtu is not used
const encoderOptions = '-preset ultrafast';
const resolution = this.determineResolution(request.video, false);
const fps = request.video.fps;
const videoBitrate = request.video.max_bit_rate;
// let fps = (this.videoConfig.forceMax && this.videoConfig.maxFPS) ||
// (request.video.fps > this.videoConfig.maxFPS) ?
// this.videoConfig.maxFPS : request.video.fps;
// let videoBitrate = (this.videoConfig.forceMax && this.videoConfig.maxBitrate) ||
// (request.video.max_bit_rate > this.videoConfig.maxBitrate) ?
// this.videoConfig.maxBitrate : request.video.max_bit_rate;
// if (vcodec === 'copy') {
// resolution.width = 0;
// resolution.height = 0;
// resolution.videoFilter = '';
// fps = 0;
// videoBitrate = 0;
// }
this.log.debug(
'Video stream requested: ' +
request.video.width +
' x ' +
request.video.height +
', ' +
request.video.fps +
' fps, ' +
request.video.max_bit_rate +
' kbps',
this.cameraName,
this.debug,
);
this.log.info(
'Starting video stream: ' +
(resolution.width > 0 ? resolution.width : 'native') +
' x ' +
(resolution.height > 0 ? resolution.height : 'native') +
', ' +
(fps > 0 ? fps : 'native') +
' fps, ' +
(videoBitrate > 0 ? videoBitrate : '???') +
' kbps',
this.cameraName,
);
// let ffmpegArgs = this.videoConfig.source;
let ffmpegArgs = `-i ${url}`;
ffmpegArgs += // Video
// (this.videoConfig.mapvideo ? ' -map ' + this.videoConfig.mapvideo : ' -an -sn -dn') +
' -an -sn -dn' +
' -codec:v ' +
vcodec +
' -pix_fmt yuv420p' +
' -color_range mpeg' +
// (fps > 0 ? ' -r ' + fps : '') +
' -f rawvideo' +
(encoderOptions ? ' ' + encoderOptions : '') +
(resolution.videoFilter.length > 0
? ' -filter:v ' + resolution.videoFilter
: '') +
(videoBitrate > 0 ? ' -b:v ' + videoBitrate + 'k' : '') +
' -payload_type ' +
request.video.pt;
ffmpegArgs += // Video Stream
' -ssrc ' +
sessionInfo.videoSSRC +
' -f rtp' +
' -srtp_out_suite AES_CM_128_HMAC_SHA1_80' +
' -srtp_out_params ' +
sessionInfo.videoSRTP.toString('base64') +
' srtp://' +
sessionInfo.address +
':' +
sessionInfo.videoPort +
'?rtcpport=' +
sessionInfo.videoPort +
'&pkt_size=' +
mtu;
if (this.audio) {
ffmpegArgs += // Audio
// (this.videoConfig.mapaudio ? ' -map ' + this.videoConfig.mapaudio : ' -vn -sn -dn') +
' -vn -sn -dn';
' -codec:a libfdk_aac' +
' -profile:a aac_eld' +
' -flags +global_header' +
' -f null' +
' -ar ' +
request.audio.sample_rate +
'k' +
' -b:a ' +
request.audio.max_bit_rate +
'k' +
' -ac ' +
request.audio.channel +
' -payload_type ' +
request.audio.pt;
ffmpegArgs += // Audio Stream
' -ssrc ' +
sessionInfo.audioSSRC +
' -f rtp' +
' -srtp_out_suite AES_CM_128_HMAC_SHA1_80' +
' -srtp_out_params ' +
sessionInfo.audioSRTP.toString('base64') +
' srtp://' +
sessionInfo.address +
':' +
sessionInfo.audioPort +
'?rtcpport=' +
sessionInfo.audioPort +
'&pkt_size=188';
}
if (this.debug) {
ffmpegArgs += ' -loglevel level+verbose';
}
const activeSession: ActiveSession = {};
activeSession.socket = createSocket(sessionInfo.ipv6 ? 'udp6' : 'udp4');
activeSession.socket.on('error', (err: Error) => {
this.log.error('Socket error: ' + err.name, this.cameraName);
this.stopStream(request.sessionID);
});
activeSession.socket.on('message', () => {
if (activeSession.timeout) {
clearTimeout(activeSession.timeout);
}
activeSession.timeout = setTimeout(() => {
this.log.info(
'Device appears to be inactive. Stopping stream.',
this.cameraName,
);
this.controller.forceStopStreamingSession(request.sessionID);
this.stopStream(request.sessionID);
}, request.video.rtcp_interval * 2 * 1000);
});
activeSession.socket.bind(
sessionInfo.videoReturnPort,
sessionInfo.localAddress,
);
activeSession.mainProcess = new FfmpegProcess(
this.cameraName,
request.sessionID,
this.videoProcessor,
ffmpegArgs,
this.log,
this.debug,
this,
callback,
);
// if (this.videoConfig.returnAudioTarget) {
// let ffmpegReturnArgs =
// '-hide_banner' +
// ' -protocol_whitelist pipe,udp,rtp,file,crypto' +
// ' -f sdp' +
// ' -c:a libfdk_aac' +
// ' -i pipe:' +
// ' ' + this.videoConfig.returnAudioTarget;
// if (this.videoConfig.debugReturn) {
// ffmpegReturnArgs += ' -loglevel level+verbose';
// }
// const ipVer = sessionInfo.ipv6 ? 'IP6' : 'IP4';
// const sdpReturnAudio =
// 'v=0\r\n' +
// 'o=- 0 0 IN ' + ipVer + ' ' + sessionInfo.address + '\r\n' +
// 's=Talk\r\n' +
// 'c=IN ' + ipVer + ' ' + sessionInfo.address + '\r\n' +
// 't=0 0\r\n' +
// 'm=audio ' + sessionInfo.audioReturnPort + ' RTP/AVP 110\r\n' +
// 'b=AS:24\r\n' +
// 'a=rtpmap:110 MPEG4-GENERIC/16000/1\r\n' +
// 'a=rtcp-mux\r\n' + // FFmpeg ignores this, but might as well
// 'a=fmtp:110 ' +
// 'profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3; ' +
// 'config=F8F0212C00BC00\r\n' +
// 'a=crypto:1 AES_CM_128_HMAC_SHA1_80 inline:' + sessionInfo.audioSRTP.toString('base64') + '\r\n';
// activeSession.returnProcess = new FfmpegProcess(this.cameraName + '] [Two-way', request.sessionID,
// this.videoProcessor, ffmpegReturnArgs, this.log, this.videoConfig.debugReturn, this);
// activeSession.returnProcess.getStdin()?.end(sdpReturnAudio);
// }
this.ongoingSessions[request.sessionID] = activeSession;
delete this.pendingSessions[request.sessionID];
});
}
Example #9
Source File: listener.ts From hoprnet with GNU General Public License v3.0 | 4 votes |
async isExposedHost(
externalIp: string,
port: number
): Promise<{
udpMapped: boolean
tcpMapped: boolean
}> {
const UDP_TEST = new TextEncoder().encode('TEST_UDP')
const TCP_TEST = new TextEncoder().encode('TEST_TCP')
const waitForIncomingUdpPacket = defer<void>()
const waitForIncomingTcpPacket = defer<void>()
const TIMEOUT = 500
const abort = new AbortController()
const tcpTimeout = setTimeout(() => {
abort.abort()
waitForIncomingTcpPacket.reject()
}, TIMEOUT)
const udpTimeout = setTimeout(waitForIncomingUdpPacket.reject.bind(waitForIncomingUdpPacket), TIMEOUT)
const checkTcpMessage = (socket: TCPSocket) => {
socket.on('data', (data: Buffer) => {
if (u8aEquals(data, TCP_TEST)) {
clearTimeout(tcpTimeout)
waitForIncomingTcpPacket.resolve()
}
})
}
this.tcpSocket.on('connection', checkTcpMessage)
const checkUdpMessage = (msg: Buffer) => {
if (u8aEquals(msg, UDP_TEST)) {
clearTimeout(udpTimeout)
waitForIncomingUdpPacket.resolve()
}
}
this.udpSocket.on('message', checkUdpMessage)
const secondUdpSocket = createSocket('udp4')
secondUdpSocket.send(UDP_TEST, port, externalIp)
let done = false
const cleanUp = (): void => {
if (done) {
return
}
done = true
clearTimeout(tcpTimeout)
clearTimeout(udpTimeout)
this.udpSocket.removeListener('message', checkUdpMessage)
this.tcpSocket.removeListener('connection', checkTcpMessage)
tcpSocket.destroy()
secondUdpSocket.close()
}
const tcpSocket = createConnection({
port,
host: externalIp,
signal: abort.signal
})
.on('connect', () => {
tcpSocket.write(TCP_TEST, (err: any) => {
if (err) {
log(`Failed to send TCP packet`, err)
}
})
})
.on('error', (err: any) => {
if (err && (err.code == undefined || err.code !== 'ABORT_ERR')) {
error(`Error while checking NAT situation`, err.message)
}
})
if (!done) {
const results = await Promise.allSettled([waitForIncomingUdpPacket.promise, waitForIncomingTcpPacket.promise])
cleanUp()
return {
udpMapped: results[0].status === 'fulfilled',
tcpMapped: results[1].status === 'fulfilled'
}
}
return {
udpMapped: false,
tcpMapped: false
}
}
Example #10
Source File: streamingDelegate.ts From homebridge-eufy-security with Apache License 2.0 | 4 votes |
private async startStream(request: StartStreamRequest, callback: StreamRequestCallback): Promise<void> {
this.videoConfig.source = '-i ' + await this.device.startStream();
const sessionInfo = this.pendingSessions.get(request.sessionID);
if (sessionInfo) {
const vcodec = this.videoConfig.vcodec || 'libx264';
const mtu = this.videoConfig.packetSize || 1316; // request.video.mtu is not used
let encoderOptions = this.videoConfig.encoderOptions;
if (!encoderOptions && vcodec === 'libx264') {
encoderOptions = '-preset ultrafast -tune zerolatency';
}
const resolution = this.determineResolution(request.video, false);
let fps = (this.videoConfig.maxFPS !== undefined &&
(this.videoConfig.forceMax || request.video.fps > this.videoConfig.maxFPS)) ?
this.videoConfig.maxFPS : request.video.fps;
let videoBitrate = (this.videoConfig.maxBitrate !== undefined &&
(this.videoConfig.forceMax || request.video.max_bit_rate > this.videoConfig.maxBitrate)) ?
this.videoConfig.maxBitrate : request.video.max_bit_rate;
if (vcodec === 'copy') {
resolution.width = 0;
resolution.height = 0;
resolution.videoFilter = undefined;
fps = 0;
videoBitrate = 0;
}
this.log.debug('Video stream requested: ' + request.video.width + ' x ' + request.video.height + ', ' +
request.video.fps + ' fps, ' + request.video.max_bit_rate + ' kbps', this.cameraName, this.videoConfig.debug);
this.log.info('Starting video stream: ' + (resolution.width > 0 ? resolution.width : 'native') + ' x ' +
(resolution.height > 0 ? resolution.height : 'native') + ', ' + (fps > 0 ? fps : 'native') +
' fps, ' + (videoBitrate > 0 ? videoBitrate : '???') + ' kbps' +
(this.videoConfig.audio ? (' (' + request.audio.codec + ')') : ''), this.cameraName);
let ffmpegArgs = this.videoConfig.source!;
ffmpegArgs += // Video
(this.videoConfig.mapvideo ? ' -map ' + this.videoConfig.mapvideo : ' -an -sn -dn') +
' -codec:v ' + vcodec +
' -pix_fmt yuv420p' +
' -color_range mpeg' +
(fps > 0 ? ' -r ' + fps : '') +
' -f rawvideo' +
(encoderOptions ? ' ' + encoderOptions : '') +
(resolution.videoFilter ? ' -filter:v ' + resolution.videoFilter : '') +
(videoBitrate > 0 ? ' -b:v ' + videoBitrate + 'k' : '') +
' -payload_type ' + request.video.pt;
ffmpegArgs += // Video Stream
' -ssrc ' + sessionInfo.videoSSRC +
' -f rtp' +
' -srtp_out_suite AES_CM_128_HMAC_SHA1_80' +
' -srtp_out_params ' + sessionInfo.videoSRTP.toString('base64') +
' srtp://' + sessionInfo.address + ':' + sessionInfo.videoPort +
'?rtcpport=' + sessionInfo.videoPort + '&pkt_size=' + mtu;
if (this.videoConfig.audio) {
if (request.audio.codec === AudioStreamingCodecType.OPUS || request.audio.codec === AudioStreamingCodecType.AAC_ELD) {
ffmpegArgs += // Audio
(this.videoConfig.mapaudio ? ' -map ' + this.videoConfig.mapaudio : ' -vn -sn -dn') +
(request.audio.codec === AudioStreamingCodecType.OPUS ?
' -codec:a libopus' +
' -application lowdelay' :
' -codec:a aac' +
' -profile:a aac_eld') +
' -flags +global_header' +
' -f null' +
' -ar ' + request.audio.sample_rate + 'k' +
' -b:a ' + request.audio.max_bit_rate + 'k' +
' -ac ' + request.audio.channel +
' -payload_type ' + request.audio.pt;
ffmpegArgs += // Audio Stream
' -ssrc ' + sessionInfo.audioSSRC +
' -f rtp' +
' -srtp_out_suite AES_CM_128_HMAC_SHA1_80' +
' -srtp_out_params ' + sessionInfo.audioSRTP.toString('base64') +
' srtp://' + sessionInfo.address + ':' + sessionInfo.audioPort +
'?rtcpport=' + sessionInfo.audioPort + '&pkt_size=188';
} else {
this.log.error('Unsupported audio codec requested: ' + request.audio.codec, this.cameraName);
}
}
ffmpegArgs += ' -loglevel level' + (this.videoConfig.debug ? '+verbose' : '') +
' -progress pipe:1';
const activeSession: ActiveSession = {};
activeSession.socket = createSocket(sessionInfo.ipv6 ? 'udp6' : 'udp4');
activeSession.socket.on('error', (err: Error) => {
this.log.error('Socket error: ' + err.message, this.cameraName);
this.stopStream(request.sessionID);
});
activeSession.socket.on('message', () => {
if (activeSession.timeout) {
clearTimeout(activeSession.timeout);
}
activeSession.timeout = setTimeout(() => {
this.log.info('Device appears to be inactive. Stopping stream.', this.cameraName);
this.controller.forceStopStreamingSession(request.sessionID);
this.stopStream(request.sessionID);
}, request.video.rtcp_interval * 5 * 1000);
});
activeSession.socket.bind(sessionInfo.videoReturnPort);
activeSession.mainProcess = new FfmpegProcess(this.cameraName, request.sessionID, this.videoProcessor,
ffmpegArgs, this.log, this.videoConfig.debug, this, callback);
if (this.videoConfig.returnAudioTarget) {
const ffmpegReturnArgs =
'-hide_banner' +
' -protocol_whitelist pipe,udp,rtp,file,crypto' +
' -f sdp' +
' -c:a aac' +
' -i pipe:' +
' ' + this.videoConfig.returnAudioTarget +
' -loglevel level' + (this.videoConfig.debugReturn ? '+verbose' : '');
const ipVer = sessionInfo.ipv6 ? 'IP6' : 'IP4';
const sdpReturnAudio =
'v=0\r\n' +
'o=- 0 0 IN ' + ipVer + ' ' + sessionInfo.address + '\r\n' +
's=Talk\r\n' +
'c=IN ' + ipVer + ' ' + sessionInfo.address + '\r\n' +
't=0 0\r\n' +
'm=audio ' + sessionInfo.audioReturnPort + ' RTP/AVP 110\r\n' +
'b=AS:24\r\n' +
'a=rtpmap:110 MPEG4-GENERIC/16000/1\r\n' +
'a=rtcp-mux\r\n' + // FFmpeg ignores this, but might as well
'a=fmtp:110 ' +
'profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3; ' +
'config=F8F0212C00BC00\r\n' +
'a=crypto:1 AES_CM_128_HMAC_SHA1_80 inline:' + sessionInfo.audioSRTP.toString('base64') + '\r\n';
activeSession.returnProcess = new FfmpegProcess(this.cameraName + '] [Two-way', request.sessionID,
this.videoProcessor, ffmpegReturnArgs, this.log, this.videoConfig.debugReturn, this);
activeSession.returnProcess.stdin.end(sdpReturnAudio);
}
this.ongoingSessions.set(request.sessionID, activeSession);
this.pendingSessions.delete(request.sessionID);
} else {
this.log.error('Error finding session information.', this.cameraName);
callback(new Error('Error finding session information'));
}
}