public static ARDSignalingMessage MessageFromJSONString(string json)
        {
            var values = JsonConvert.DeserializeObject <Dictionary <string, string> >(json);
            ARDSignalingMessage message = new ARDSignalingMessage();
            var type = values["type"];

            if (type == "candidate")
            {
                RTCICECandidate candidate = new RTCICECandidate(values["id"], nint.Parse(values["label"]), values["candidate"]);
                message = new ARDICECandidateMessage(candidate);
            }
            else if (type == "offer" || type == "answer")
            {
                RTCSessionDescription description = new RTCSessionDescription(type, values["sdp"]);
                message = new ARDSessionDescriptionMessage(description);
            }
            else if (type == "bye")
            {
                message = new ARDByeMessage();
            }
            else
            {
                System.Diagnostics.Debug.WriteLine($"Unexpected type: {type}");
            }
            return(message);
        }
        protected virtual void OnReceivedMessage(string message)
        {
            SocketResponse socketResponse;

            try
            {
                socketResponse = JsonConvert.DeserializeObject <SocketResponse>(message);
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Invalid json error: {ex.Message} message:{message}");
                return;
            }

            if (!string.IsNullOrEmpty(socketResponse.error))
            {
                Debug.WriteLine($"WSS error: {socketResponse.error}");
                return;
            }

            var payload = ARDSignalingMessage.MessageFromJSONString(socketResponse.msg);

            Debug.WriteLine($"WSS->C: {payload}");

            Delegate?.DidReceiveMessage(this, payload);
        }
        public override void SendMessage(ARDSignalingMessage message)
        {
            Contract.Requires(!string.IsNullOrEmpty(RoomId));
            var data    = message.JsonData;
            var payload = data != null ? new NSString(data, NSStringEncoding.UTF8) : new NSString("{}");

            switch (State)
            {
            case ARDSignalingChannelState.Registered:
                var messageDict = new NSDictionary(
                    "cmd", "send",
                    "msg", payload);

                var messageJSON = NSJsonSerialization.Serialize(
                    messageDict, NSJsonWritingOptions.PrettyPrinted, out NSError err);

                var messageString = new NSString(
                    messageJSON, NSStringEncoding.UTF8);
                SendMessage(messageString);
                break;

            default:
                Debug.WriteLine($"C->WSS POST:{payload}");
                var url = new NSUrl(WebRestFormated);
                url.SendAsyncPostToURL(data, null);
                break;
            }
        }
        void ProcessSignalingMessage(ARDSignalingMessage message)
        {
            switch (message.Type)
            {
            case ARDSignalingMessageType.Offer:
            case ARDSignalingMessageType.Answer:
                ARDSessionDescriptionMessage sdpMessage = message as ARDSessionDescriptionMessage;
                _peerConnection.SetRemoteDescriptionWithDelegate(this, sdpMessage.Description);
                break;

            case ARDSignalingMessageType.Candidate:
                ARDICECandidateMessage candidateMessage = message as ARDICECandidateMessage;
                _peerConnection.AddICECandidate(candidateMessage.Candidate);
                break;

            case ARDSignalingMessageType.Bye:
                // Other client disconnected.
                // TODO(tkchin): support waiting in room for next client. For now just
                Disconnect();
                break;

            default:
                break;
            }
        }
        void SendSignalingMessageToCollider(ARDSignalingMessage message)
        {
            NSData data = null;

            _channel.SendData(data);
            //NSData* data = [message JSONData];
        }
Example #6
0
        public static ARDSignalingMessage MessageFromJSONString(string json)
        {
            var values = JsonConvert.DeserializeObject <Dictionary <string, string> >(json);
            ARDSignalingMessage message = new ARDSignalingMessage();
            var type = values["type"];

            switch (type)
            {
            case "candidate":
                nint.TryParse(values["label"], out nint label);
                RTCICECandidate candidate = new RTCICECandidate(values["id"], label, values["candidate"]);
                message = new ARDICECandidateMessage(candidate);
                break;

            case "offer":
            case "answer":
                RTCSessionDescription description = new RTCSessionDescription(type, values["sdp"]);
                message = new ARDSessionDescriptionMessage(description);
                break;

            case "bye":
                message = new ARDByeMessage();
                break;

            default:
                System.Diagnostics.Debug.WriteLine($"Unexpected type: {type}");
                break;
            }

            return(message);
        }
        async Task SendSignalingMessageToRoomServer(ARDSignalingMessage message, Action <ARDMessageResponse> completionHandler = null)
        {
            var    jsonData  = message.JsonData;
            string urlString = string.Format(kARDRoomServerMessageFormat, _serverHostUrl, _roomId, _clientId);

            System.Diagnostics.Debug.WriteLine($"C->RS POST: {jsonData}");
            var client = new HttpClient {
                BaseAddress = new Uri(urlString)
            };
            ARDMessageResponse responseMessage = null;
            NSError            error           = null;

            try
            {
                var response = await client.PostAsync("", new StringContent(jsonData));

                if (!response.IsSuccessStatusCode)
                {
                    error = RoomServerNetworkError(1);
                    _delegate.DidError(this, error);
                    return;
                }
                var responseContent = await response.Content.ReadAsStringAsync();

                responseMessage = JsonConvert.DeserializeObject <ARDMessageResponse>(responseContent);
                switch (responseMessage.Type)
                {
                case ARDMessageResultType.Success:
                    break;

                case ARDMessageResultType.Unknown:
                    error = RoomServerNetworkError(kARDAppClientErrorUnknown);
                    break;

                case ARDMessageResultType.InvalidClient:
                    error = RoomServerNetworkError(kARDAppClientErrorInvalidClient);
                    break;

                case ARDMessageResultType.InvalidRoom:
                    error = RoomServerNetworkError(kARDAppClientErrorInvalidRoom);
                    break;

                default:
                    break;
                }
            }
            catch (Exception ex)
            {
                error = RoomServerNetworkError(kARDAppClientErrorUnknown, ex.Message);
            }
            if (error != null)
            {
                _delegate.DidError(this, error);
            }
            completionHandler?.Invoke(responseMessage);
        }
 async Task SendSignalingMessage(ARDSignalingMessage message)
 {
     if (_isInitiator)
     {
         await SendSignalingMessageToRoomServer(message);
     }
     else
     {
         SendSignalingMessageToCollider(message);
     }
 }
Example #9
0
        public async Task <ARDMessageResponse> SendMessageAsync(ARDSignalingMessage message, string roomId, string clientId)
        {
            var data = message.JsonData;

            var urlString = string.Format(kARDRoomServerMessageFormat, roomId, clientId);

            Console.WriteLine("C->RS POST: {0}", message);

            var response = await _httpClient.PostAsync(urlString, new StringContent(data.ToString(), Encoding.UTF8, "application/json")).ConfigureAwait(false);

            var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            return(JsonConvert.DeserializeObject <ARDMessageResponse>(json));
        }
Example #10
0
        private void AddInternal(ARDSignalingMessage message)
        {
            switch (message.Type)
            {
            case ARDSignalingMessageType.Answer:
            case ARDSignalingMessageType.Offer:
                HasReceivedSdp = true;
                _messageQueue.Insert(0, message);
                break;

            default:
                _messageQueue.Add(message);
                break;
            }
        }
Example #11
0
        public static ARDSignalingMessage MessageFromJSONString(string json)
        {
            var values = JsonConvert.DeserializeObject <Dictionary <string, string> >(json);
            ARDSignalingMessage message = new ARDSignalingMessage();

            if (values.ContainsKey("type"))
            {
                var type = values["type"] ?? "";
                switch (type)
                {
                case "candidate":
                    int.TryParse(values["label"], out int label);
                    RTCIceCandidate candidate = new RTCIceCandidate(values["id"], label, values["candidate"]);
                    message = new ARDICECandidateMessage(candidate);
                    break;

                case "offer":
                    RTCSessionDescription description = new RTCSessionDescription(RTCSdpType.Offer, values["sdp"]);
                    message = new ARDSessionDescriptionMessage(description);
                    break;

                case "answer":
                    description = new RTCSessionDescription(RTCSdpType.Answer, values["sdp"]);
                    message     = new ARDSessionDescriptionMessage(description);
                    break;

                case "bye":
                    message = new ARDByeMessage();
                    break;

                default:
                    System.Diagnostics.Debug.WriteLine($"ARDSignalingMessage unexpected type: {type}");
                    break;
                }
            }
            else
            {
                System.Diagnostics.Debug.WriteLine($"ARDSignalingMessage invalid json: {json}");
            }

            return(message);
        }
        public void DidReceiveMessage(ARDSignalingMessage message)
        {
            switch (message.Type)
            {
            case ARDSignalingMessageType.Offer:
            case ARDSignalingMessageType.Answer:
                _hasReceivedSdp = true;
                _messageQueue.Insert(0, message);
                break;

            case ARDSignalingMessageType.Candidate:
                _messageQueue.Add(message);
                break;

            case ARDSignalingMessageType.Bye:
                ProcessSignalingMessage(message);
                break;

            default:
                break;
            }
            DrainMessageQueueIfReady();
        }
Example #13
0
        //didReceiveMessage
        public override void WebSocket(SRWebSocket webSocket, NSObject message)
        {
            SocketResponse socketResponse;

            try
            {
                socketResponse = JsonConvert.DeserializeObject <SocketResponse>(message.ToString());
                if (!string.IsNullOrEmpty(socketResponse.error))
                {
                    System.Diagnostics.Debug.WriteLine($"WSS error: {socketResponse.error}");
                    return;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"Invalid json  error: {ex.Message}");
                return;
            }

            System.Diagnostics.Debug.WriteLine(@"WSS->C: %@", socketResponse.msg);
            ARDSignalingMessage signalingMessage = ARDSignalingMessageExtensions.MessageFromJSONString(socketResponse.msg);

            _delegate.DidReceiveMessage(signalingMessage);
        }
Example #14
0
 public void Add(ARDSignalingMessage message)
 {
     AddInternal(message);
     DrainMessageQueueIfReady();
 }
Example #15
0
 public void DidReceiveMessage(ARDSignalingChannel channel, ARDSignalingMessage message)
 {
     _messageQueue.Add(message);
     _messageQueue.DrainMessageQueueIfReady();
 }