将Byte数据发送到SignalR方法时,WebSocket已处于CLOSING或CLOSED状态错误

时间:2016-12-29 21:20:21

标签: javascript c# websocket audio-streaming signalr-hub

我正在尝试将音频发送到服务器。我想要完成的是将一个字节流发送到服务器。我目前的尝试是使用SignalR。在服务器上进行JSON解析后出现了很多问题(我最后通过添加Int8Array行来“修复”)。

现在我有以下错误:

[16:15:19 GMT-0500 (Eastern Standard Time)] SignalR: Invoking myhub.SendAudio
jquery.signalR-2.2.1.min.js:9 [16:15:19 GMT-0500 (Eastern Standard Time)] SignalR: Unclean disconnect from  websocket: [no reason given].
jquery.signalR-2.2.1.min.js:9 [16:15:19 GMT-0500 (Eastern Standard Time)] SignalR: Invoking myhub.SendAudio

然后这一遍又一遍(约11次):

[15:06:54 GMT-0500 (Eastern Standard Time)] SignalR: Invoking myhub.SendAudio
WebSocket is already in CLOSING or CLOSED state.

最终:

[16:11:06 GMT-0500 (Eastern Standard Time)] SignalR: Closing the Websocket.
jquery.signalR-2.2.1.min.js:9 [16:11:06 GMT-0500 (Eastern Standard Time)] SignalR: Clearing hub invocation callbacks with error: Connection started reconnecting before invocation result was received..
jquery.signalR-2.2.1.min.js:9 [16:11:06 GMT-0500 (Eastern Standard Time)] SignalR: myhub.SendAudio failed to execute. Error: Connection started reconnecting before invocation result was received.

如果我将byte []更改为字符串,它就可以正常工作。如果我拿出我的“调试器”;它会在重新连接后重复所有错误。只有最后一个错误触发调试器,其他错误

这是Javascript:

var audioCtx = new (window.AudioContext || window.webkitAudioContext)();

var hub = $.connection.myHub;
$.connection.hub.logging = true;

$.connection.hub.start().done(function () {
    // I know that I should leverage this method, 
    //  but I am clicking a button to start recording for now, so it isn't an issue.
});


var recorderProcess = function (e) {
    var left = e.inputBuffer.getChannelData(0);
    var array = new Int8Array(left.buffer); // this is just the latest attempt
    var obj = { buffer: array };
    hub.server.sendAudio(obj).done(function (text) {
        console.log(text);
        txtResult.val(txtResult.val() + ' ' + text);
    }).fail(function (error) {
        debugger;
    });
}

var initializeRecorder = function (stream) {
    var context = audioCtx;
    var audioInput = context.createMediaStreamSource(stream);

    var bufferSize = 2048;
    var recorder = context.createScriptProcessor(bufferSize, 1, 1);
    recorder.onaudioprocess = recorderProcess;
    audioInput.connect(recorder); 
    recorder.connect(context.destination); 
}

$('#btnPlay').click(function () {
    navigator.getUserMedia(session, initializeRecorder, onError);
}

服务器端代码:

public class AudioData {
    [JsonProperty("buffer")]
    public byte[] Buffer { get; set; }
}

public class MyHub : Hub {
    public async Task<string> SendAudio(AudioData data) {
        byte[] audioData = data.Buffer;
        // I try and do things here, but I haven't been able to get it to work that far.
    }

}

在初创公司

var hubConfiguration = new HubConfiguration();
hubConfiguration.EnableDetailedErrors = true;
app.MapSignalR("/signalr", hubConfiguration);

0 个答案:

没有答案