i want stream audio buffers javascript flask server, using socket.io. socket instance 'undefined' when want use within audio callback function.
in code, socket instance in main thread 'socket'. function 'record' called worker every time new buffer received microphone.
but socket.emit gives me:
uncaught referenceerror: socket not definedrecord @ blob:http://localhost:7777/400a58bc-b64f-4c66-9bb1-65a28bf9bfba:39onmessage @ blob:http://localhost:7777/400a58bc-b64f-4c66-9bb1-65a28bf9bfba:15
how can access socket correctly within worker?
in main thread, have socket object , method called in postmessage web worker:
var socket = io.connect('http://' + document.domain + ':'+location.port); socket.on('connect', function() { socket.emit('my event', {data: 'i\'m connected!'}); }); ... function record(inputbuffer){ var buf = inputbuffer[0] ///////here socket cannot found, why?? socket.emit('audio event',{data : buf}) }
in web worker thread, postmessage calls 'record' method:
this.onmessage = function(e){ switch(e.data.command){ case 'record': /////here 'record' function called in main thread record(e.data.buffer); break; } }; ... (function(window){ var recorder = function(source, cfg){ this.context = source.context; if(!this.context.createscriptprocessor){ this.node = this.context.createjavascriptnode(bufferlen, 2, 2); } else { this.node = this.context.createscriptprocessor(bufferlen, 2, 2); } var currcallback; this.node.onaudioprocess = function(e){ ////this callback microphone hardware worker.postmessage({ command: 'record', buffer: [ e.inputbuffer.getchanneldata(0), e.inputbuffer.getchanneldata(1) ] }); } this.record = function(){ recording = true; } worker.onmessage = function(e){ var blob = e.data; currcallback(blob); } window.recorder = recorder; })(window);
if socket in main thread, can't access webworker. that's limitation of webworkers. cannot share variables between main thread , webworker. limitation exists because of concurrency issues.
your choices here be:
create websocket in webworker
socket
variable exists in webworker , can used there.when want send data websocket within webworker, send message main thread data argument asking send data on websocket , have main thread actual sending. so, sending on websocket stay in main thread.
Comments
Post a Comment