예제 #1
0
 void Awake()
 {
     SIPSorcery.LogFactory.Set(new UnityLoggerFactory());
     _webRtcPeer = new WebRTCPeer();
 }
예제 #2
0
        async void OnConnectButtonClicked(object sender, EventArgs args)
        {
            logger.LogDebug($"Attempting to connection to web socket at {this._webSocketURL.Text}.");

            var clientWebSocket = new ClientWebSocket();

            if (!Uri.TryCreate(this._webSocketURL.Text, UriKind.Absolute, out var uri))
            {
                this._status.Text = "Invalid web socket URI.";
            }
            else
            {
                this._connectButton.IsVisible = false;
                this._closeButton.IsVisible   = true;

                this._status.Text = "Attempting to connect to web socket server.";

                await clientWebSocket.ConnectAsync(uri, CancellationToken.None);

                var buffer   = WebSocket.CreateClientBuffer(8192, 8192);
                int attempts = 0;

                while (true && attempts < 10)
                {
                    WebSocketReceiveResult response = await clientWebSocket.ReceiveAsync(buffer, CancellationToken.None);

                    if (response.EndOfMessage)
                    {
                        _peer = new WebRTCPeer("peer1", DATA_CHANNEL_LABEL);

                        _peer.PeerConnection.oniceconnectionstatechange += (state) => Device.BeginInvokeOnMainThread(() => this._status.Text = $"ICE connection state {state}.");
                        _peer.PeerConnection.onconnectionstatechange    += (state) => Device.BeginInvokeOnMainThread(() => this._status.Text = $"Peer connection state {state}.");
                        _peer.OnDataChannelMessage += (msg_) => Device.BeginInvokeOnMainThread(() => this._dataChannelMessages.Text += $"\n{msg_}");

                        var options = new JsonSerializerOptions();
                        options.Converters.Add(new JsonStringEnumConverter());
                        var init = JsonSerializer.Deserialize <RTCSessionDescriptionInit>(buffer.Take(response.Count).ToArray(), options);
                        _peer.PeerConnection.setRemoteDescription(init);

                        var answer = _peer.PeerConnection.createAnswer(null);
                        await _peer.PeerConnection.setLocalDescription(answer);

                        var answerJson = JsonSerializer.Serialize <RTCSessionDescriptionInit>(answer, options);
                        await clientWebSocket.SendAsync(
                            new ArraySegment <byte>(System.Text.Encoding.UTF8.GetBytes(answerJson)),
                            WebSocketMessageType.Text, true, CancellationToken.None);

                        attempts = 10;
                        while (_peer.PeerConnection.connectionState == RTCPeerConnectionState.connecting && attempts < 10)
                        {
                            await Task.Delay(1000);

                            attempts++;
                        }

                        break;
                    }
                    else
                    {
                        logger.LogWarning("Failed to get full web socket message from server.");

                        this._status.Text = "Web socket message exchange failed.";
                    }

                    attempts++;
                }
            }
        }
예제 #3
0
        async void OnConnectButtonClicked(object sender, EventArgs args)
        {
            logger.LogDebug($"Attempting to connection to web socket at {this._webSocketURL.Text}.");

            if (!Uri.TryCreate(this._webSocketURL.Text, UriKind.Absolute, out var uri))
            {
                this._status.Text = "Invalid web socket URI.";
            }
            else
            {
                this._connectButton.IsVisible = false;
                this._closeButton.IsVisible   = true;

                this._status.Text = "Attempting to connect to web socket server.";

                try
                {
                    _peer = new WebRTCPeer("peer1", DATA_CHANNEL_LABEL, uri);
                    await _peer.Connect(CancellationToken.None);

                    if (_peer.PeerConnection.connectionState == RTCPeerConnectionState.connected)
                    {
                        _isWebRTCConnected = true;
                        this._status.Text  = "WebRTC peer connection successfully established.";
                        _peer.PeerConnection.onconnectionstatechange += (state) => Device.BeginInvokeOnMainThread(() => this._status.Text = $"Peer connection state {state}.");
                    }
                    else
                    {
                        this._status.Text = "Web socket connection successful, WebRTC peer connecting...";

                        _peer.PeerConnection.onconnectionstatechange += (connState) =>
                        {
                            if (_peer.PeerConnection.connectionState == RTCPeerConnectionState.connected)
                            {
                                if (!_isWebRTCConnected)
                                {
                                    _isWebRTCConnected = true;

                                    this.Dispatcher.BeginInvokeOnMainThread(() => this._status.Text = "WebRTC peer connection successfully established.");

                                    _peer.PeerConnection.onconnectionstatechange += (state) => Device.BeginInvokeOnMainThread(() => this._status.Text = $"Peer connection state {state}.");
                                    _peer.OnDataChannelMessage += (msg_) => Device.BeginInvokeOnMainThread(() => this._dataChannelMessages.Text += $"\n{msg_}");
                                }
                            }
                            else if (_peer.PeerConnection.connectionState == RTCPeerConnectionState.failed ||
                                     _peer.PeerConnection.connectionState == RTCPeerConnectionState.closed)
                            {
                                this.Dispatcher.BeginInvokeOnMainThread(() => this._status.Text = $"WebRTC peer connection attempt failed in state {_peer.PeerConnection.connectionState}.");
                            }
                        };
                    }
                }
                catch (Exception excp)
                {
                    this._status.Text = $"Error connecting. {excp.Message}";

                    this._closeButton.IsVisible   = false;
                    this._connectButton.IsVisible = true;
                }
            }
        }