EntertainmentStream.js
3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
'use strict';
// const dtls = require('@nodertc/dtls')
// const dtls = require('node-dtls-client').dtls
const dtls = require('node-mbed-dtls-client')
, ApiError = require('../../ApiError')
;
module.exports = class EntertainmentStream {
constructor(ipAddress, username, clientkey) {
// this._config = {
// type: 'udp4',
// remotePort: 2100,
// remoteAddress: ipAddress,
// pskSecret: getPSK(clientkey),
// pskIdentity: username,
// cipherSuites: ['TLS_PSK_WITH_AES_128_GCM_SHA256'],
// };
// this._config = {
// type: 'udp4',
// port: 2100,
// address: ipAddress,
// psk: {
// psk_identity: username,
// psk: getPSK(clientkey)
// },
// cipherSuites: ['TLS_PSK_WITH_AES_128_GCM_SHA256'],
// };
this._config = {
debug: 10,
// type: 'udp4',
port: 2100,
host: ipAddress,
psk: getPSK(clientkey),
// psk_identity: Buffer.from(username, 'ascii'),
psk_identity: username,
// cipherSuites: ['TLS_PSK_WITH_AES_128_GCM_SHA256'],
};
}
connect() {
const self = this;
const promise = new Promise((resolve, reject) => {
const socket = dtls.connect(this._config);
// const socket = dtls.createSocket(this._config);
// socket.once('error', err => {
socket.on('error', err => {
console.error(err); //TODO
reject(new ApiError(err));
// socket.close();
});
socket.on('drain', () => {
console.error('draining')
});
socket.on('pipe', () => {
console.error('pipe')
});
socket.on('finish', () => {
console.error('finished')
});
socket.once('connect', () => {
console.error('Connected!');//TODO
self._socket = socket;
// socket.on('error', err => {
// console.error(err);
// });
resolve(true);
});
});
return promise;
}
send(data) {
const self = this
, socket = self._socket
;
const result = socket.write(data, () => {
console.log(`Wrote Data: ${data}`);
});//TODO can use a callback to confirm data
// socket.end();
return result;
}
close() {
const self = this
, socket = self._socket
, promise = new Promise((resolve, reject) => {
if (socket) {
console.error(`Socket Bytes written: ${socket.bytesWritten}`);
socket.close();
resolve(true);
} else {
reject(new ApiError('There is currently no valid socket open to close.'));
}
});
return promise;
}
};
function hexToBytes(hex) {
// console.log(hex);
//
// const hexBuf = Buffer.from(hex, 'hex');
// return hexBuf;
// return parseInt(hex, 16);
// 32 character ascii to 16 byte binary representation
const arr = [];
for (let c = 0; c < hex.length; c += 2) {
arr.push(parseInt(hex.substr(c, 2), 16));
}
console.log(arr);
return Buffer.from(arr, 'binary');
// const arr = new ArrayBuffer(16)
// , view = new DataView(arr)
// ;
//
// let offset = 0;
//
// for (let c = 0; c < hex.length; c += 2) {
// const intVal = parseInt(hex.substr(c, 2), 16);
// view.setUint16(offset, intVal, false);
// offset++;
// }
//
// return Buffer.from(arr);
}
function getPSK(val) {
// const result = hexToBytes(val);
// console.log(result);
// return Buffer.from(result);
const hexBuff = Buffer.from(val, 'hex');
console.log(hexBuff);
return hexBuff;
}