wrapped websocket connection + launch with argument 'wsAddress'

This commit is contained in:
xdrm-brackets 2018-04-06 01:42:56 +02:00
parent 5ed03574a7
commit 0a4b71b321
1 changed files with 46 additions and 47 deletions

View File

@ -30,50 +30,8 @@ export default class AudioManager{
/* (6) Create output + bind gain */ /* (6) Create output + bind gain */
this.output = this.ctx.destination; this.output = this.ctx.destination;
/* (7) Create websocket connection */ /* (7) Initialise websocket */
this.ws = new WebSocket('wss://ws.douscord.xdrm.io/audio/2'); this.ws = null;
/* (8) Manage websocket requests */
this._ws = {
stack: [],
send: function(_data){
if( this.ws.readyState !== 1 ){ // not connected -> stack
console.warn(`push stack`);
return this._ws.stack.push(_data);
}
console.warn(`send buffer[${_data.length}]`);
this.ws.send(_data);
}.bind(this)
};
/* (9) Manage websocket message stack */
this.ws.onopen = function(){
console.warn(`pop stack of size ${this._ws.stack.length}`);
while( this._ws.stack.length > 0 )
this.ws.send(this._ws.stack.shift());
}.bind(this);
/* (10) Manage websocket responses */
this.ws.onmessage = function(_msg){
console.warn(`received`, _msg.data);
if( !(_msg.data instanceof Blob) )
return console.warn('NIQUE');
let fr = new FileReader();
fr.onload = function(){
let buf16 = new Int16Array(fr.result);
this.receive(buf16);
}.bind(this);
fr.readAsArrayBuffer(_msg.data);
}.bind(this);
@ -140,8 +98,11 @@ export default class AudioManager{
let buf16 = this.f32toi16(buf32); let buf16 = this.f32toi16(buf32);
console.log(`read buffer[${buf16.length}]`); if( this.ws )
this._ws.send(buf16); console.log(`send buffer[${buf16.length}]`);
else
console.log(`websocket not connected`);
this.ws && this.ws.send(buf16);
} }
@ -214,10 +175,48 @@ export default class AudioManager{
} }
/* (8) Connect websocket
*
* @address<String> Websocket address
*
---------------------------------------------------------*/
wsconnect(_addr){
/* (1) Create websocket connection */
this.ws = new WebSocket(_addr);
/* (2) Manage websocket responses */
this.ws.onmessage = function(_msg){
console.warn(`received`, _msg.data);
if( !(_msg.data instanceof Blob) )
return console.warn('[NaB] Not A Blob');
let fr = new FileReader();
fr.onload = function(){
let buf16 = new Int16Array(fr.result);
this.receive(buf16);
}.bind(this);
fr.readAsArrayBuffer(_msg.data);
}.bind(this);
}
/* (x) Access microphone + launch all /* (x) Access microphone + launch all
* *
---------------------------------------------------------*/ ---------------------------------------------------------*/
launch(){ launch(wsAddress='wss://ws.douscord.xdrm.io/audio/2'){
/* (1) Start websocket */
this.wsconnect(wsAddress);
window.recorder = null; window.recorder = null;