/// <summary> /// Initializes a new instance of the <see cref="T:WebAssembly.Net.WebSockets.ClientWebSocket"/> class. /// </summary> public RTCPeerConnection(RTCPeerConfiguration configuration = null) { state = created; cts = new CancellationTokenSource(); var rtcPeerConfiguration = new JSObject(); var json = (JSObject)System.Runtime.InteropServices.JavaScript.Runtime.GetGlobalObject("JSON"); var config = json.Invoke("parse", "{\"iceServers\":[{\"urls\":\"stun:stun.l.google.com:19302\"}]}"); innerRtcPeerConnection = new HostObject("RTCPeerConnection", config); onIcecandidate = new Action <JSObject>((e) => { //var json = e.Invoke("toJSON"); var c = e.GetObjectProperty("candidate") as JSObject; RTCIceCandidate iceCandidate = null; if (c != null) { iceCandidate = new RTCIceCandidate(); iceCandidate.candidate = c?.GetObjectProperty("candidate") as string; iceCandidate.sdpMid = c?.GetObjectProperty("sdpMid") as string; iceCandidate.sdpMLineIndex = (int)c?.GetObjectProperty("sdpMLineIndex"); Console.WriteLine(iceCandidate.candidate); } OnIceCandidate?.Invoke(this, new RTCIceCandidateInit() { candidate = iceCandidate }); e.Dispose(); }); // Attach the onError callback innerRtcPeerConnection.SetObjectProperty("onicecandidate", onIcecandidate); // Setup the onOpen callback onDataChannel = new Action <JSObject>((evt) => { var channel = (JSObject)evt.GetObjectProperty("channel"); var dc = new RTCDataChannel(channel); OnDataChannel?.Invoke(this, dc); evt.Dispose(); }); // Attach the onOpen callback innerRtcPeerConnection.SetObjectProperty("ondatachannel", onDataChannel); onNegotiationNeeded = new Action <JSObject>((e) => { OnNegotiationNeeded?.Invoke(this, EventArgs.Empty); e.Dispose(); }); // Attach the onError callback innerRtcPeerConnection.SetObjectProperty("onnegotiationneeded", onNegotiationNeeded); onConnectionstatechange = new Action <JSObject>((e) => { OnConnectionstatechange?.Invoke(this, EventArgs.Empty); e.Dispose(); }); // Attach the onError callback innerRtcPeerConnection.SetObjectProperty("onconnectionstatechange", onConnectionstatechange); onIcegatheringstatechange = new Action <JSObject>((e) => { var s = this.iceGatheringState; e.Dispose(); }); // Attach the onError callback innerRtcPeerConnection.SetObjectProperty("onicegatheringstatechange", onIcegatheringstatechange); onIcecandidateerror = new Action <JSObject>((e) => { e.Dispose(); }); innerRtcPeerConnection.Invoke("addEventListener", "icecandidateerror", onIcecandidateerror); onIceconnectionstatechange = new Action <JSObject>((e) => { e.Dispose(); }); innerRtcPeerConnection.Invoke("addEventListener", "icecandidateerror", onIceconnectionstatechange); onSignalingstatechange = new Action <JSObject>((e) => { Console.WriteLine("state:" + this.SignalingState); e.Dispose(); }); innerRtcPeerConnection.Invoke("addEventListener", "signalingstatechange", onSignalingstatechange); }
/// <summary> /// Initializes a new instance of the <see cref="T:WebAssembly.Net.WebSockets.ClientWebSocket"/> class. /// </summary> public RTCPeerConnection() { state = created; cts = new CancellationTokenSource(); innerRtcPeerConnection = new HostObject("RTCPeerConnection"); onIcecandidate = new Action <JSObject>((e) => { //var json = e.Invoke("toJSON"); var c = e.GetObjectProperty("candidate") as JSObject; RTCIceCandidate iceCandidate = null; if (c != null) { iceCandidate = new RTCIceCandidate(); iceCandidate.candidate = c?.GetObjectProperty("candidate") as string; iceCandidate.sdpMid = c?.GetObjectProperty("sdpMid") as string; iceCandidate.sdpMLineIndex = (int)c?.GetObjectProperty("sdpMLineIndex"); } OnIceCandidate?.Invoke(this, new RTCIceCandidateInit() { candidate = iceCandidate }); e.Dispose(); }); // Attach the onError callback innerRtcPeerConnection.SetObjectProperty("onicecandidate", onIcecandidate); // Setup the onOpen callback onDataChannel = new Action <JSObject>((evt) => { var channel = (JSObject)evt.GetObjectProperty("channel"); var dc = new RTCDataChannel(channel); OnDataChannel?.Invoke(this, dc); evt.Dispose(); }); // Attach the onOpen callback innerRtcPeerConnection.SetObjectProperty("ondatachannel", onDataChannel); onNegotiationNeeded = new Action <JSObject>((e) => { OnNegotiationNeeded?.Invoke(this, EventArgs.Empty); e.Dispose(); }); // Attach the onError callback innerRtcPeerConnection.SetObjectProperty("onnegotiationneeded", onNegotiationNeeded); onConnectionstatechange = new Action <JSObject>((e) => { OnConnectionstatechange?.Invoke(this, EventArgs.Empty); e.Dispose(); }); // Attach the onError callback innerRtcPeerConnection.SetObjectProperty("onconnectionstatechange", onConnectionstatechange); onIcegatheringstatechange = new Action <JSObject>((e) => { var s = this.iceGatheringState; e.Dispose(); }); // Attach the onError callback innerRtcPeerConnection.SetObjectProperty("onicegatheringstatechange", onIcegatheringstatechange); onIcecandidateerror = new Action <JSObject>((e) => { e.Dispose(); }); innerRtcPeerConnection.Invoke("addEventListener", "icecandidateerror", onIcecandidateerror); onIceconnectionstatechange = new Action <JSObject>((e) => { e.Dispose(); }); innerRtcPeerConnection.Invoke("addEventListener", "icecandidateerror", onIceconnectionstatechange); onSignalingstatechange = new Action <JSObject>((e) => { Console.WriteLine("state:" + this.SignalingState); e.Dispose(); }); innerRtcPeerConnection.Invoke("addEventListener", "signalingstatechange", onSignalingstatechange); }
private async Task <IJSInProcessObjectReference> Init() { var loaded = await moduleTask.Value; if (configuration == null) { innerRtcPeerConnection = new HostObject("RTCPeerConnection"); } else { await loaded.InvokeVoidAsync("init", DotNetObjectReference.Create(this)); var window = (JSObject)System.Runtime.InteropServices.JavaScript.Runtime.GetGlobalObject("window"); //var options = new JsonSerializerOptions //{ // DefaultIgnoreCondition = Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull, // PropertyNamingPolicy = JsonNamingPolicy.CamelCase //}; //var json = System.Text.Json.JsonSerializer.Serialize(configuration, options); var desc = (JSObject)window.Invoke("CreatePeerConnection"); innerRtcPeerConnection = desc; } onIcecandidate = new Action <JSObject>((e) => { //var json = e.Invoke("toJSON"); var c = e.GetObjectProperty("candidate") as JSObject; RTCIceCandidate iceCandidate = null; if (c != null) { iceCandidate = new RTCIceCandidate(); iceCandidate.candidate = c?.GetObjectProperty("candidate") as string; iceCandidate.sdpMid = c?.GetObjectProperty("sdpMid") as string; iceCandidate.sdpMLineIndex = (int)c?.GetObjectProperty("sdpMLineIndex"); } OnIceCandidate?.Invoke(this, new RTCIceCandidateInit() { candidate = iceCandidate }); e.Dispose(); }); // Attach the onError callback innerRtcPeerConnection.SetObjectProperty("onicecandidate", onIcecandidate); // Setup the onOpen callback onDataChannel = new Action <JSObject>((evt) => { var channel = (JSObject)evt.GetObjectProperty("channel"); var dc = new RTCDataChannel(channel); OnDataChannel?.Invoke(this, dc); evt.Dispose(); }); // Attach the onOpen callback innerRtcPeerConnection.SetObjectProperty("ondatachannel", onDataChannel); onNegotiationNeeded = new Action <JSObject>((e) => { OnNegotiationNeeded?.Invoke(this, EventArgs.Empty); e.Dispose(); }); // Attach the onError callback innerRtcPeerConnection.SetObjectProperty("onnegotiationneeded", onNegotiationNeeded); onConnectionstatechange = new Action <JSObject>((e) => { OnConnectionstatechange?.Invoke(this, EventArgs.Empty); e.Dispose(); }); // Attach the onError callback innerRtcPeerConnection.SetObjectProperty("onconnectionstatechange", onConnectionstatechange); onIcegatheringstatechange = new Action <JSObject>((e) => { var s = this.iceGatheringState; e.Dispose(); }); // Attach the onError callback innerRtcPeerConnection.SetObjectProperty("onicegatheringstatechange", onIcegatheringstatechange); onIcecandidateerror = new Action <JSObject>((e) => { e.Dispose(); }); innerRtcPeerConnection.Invoke("addEventListener", "icecandidateerror", onIcecandidateerror); onIceconnectionstatechange = new Action <JSObject>((e) => { e.Dispose(); }); innerRtcPeerConnection.Invoke("addEventListener", "oniceconnectionstatechange", onIceconnectionstatechange); onSignalingstatechange = new Action <JSObject>((e) => { Console.WriteLine("state:" + this.SignalingState); e.Dispose(); }); innerRtcPeerConnection.Invoke("addEventListener", "signalingstatechange", onSignalingstatechange); return(loaded); }