Esempio n. 1
0
        public string GetBssid()
        {
            var res = FormWifiManager.GetBssId();

            NetworkServiceUtil.Log("Socket GetBssid");
            return(res);
        }
Esempio n. 2
0
        private async Task <string> GetWsData(string endPoint, string deviceInfoRequest, string defaultResponse = null)
        {
            NetworkServiceUtil.Log("Socket GetWsData: start");
            try
            {
                if (deviceInfoRequest != null)
                {
                    NetworkServiceUtil.Log("Socket GetWsData:1");
                    client = await InitClientWebSocket(endPoint);

                    NetworkServiceUtil.Log("Socket GetWsData:2");
                    await WifiAdapter.Instance.SendMessageAsync(deviceInfoRequest, client);

                    NetworkServiceUtil.Log("Socket GetWsData:3");
                    var result = await WifiAdapter.Instance.ReadMessage(client);

                    NetworkServiceUtil.Log("Socket GetWsData:4");
                    return(result);
                }
            }
            catch (Exception e)
            {
                NetworkServiceUtil.Log("Socket GetWsData: Exception");
                System.Diagnostics.Debug.WriteLine(e);
                return(defaultResponse);
            }
            finally
            {
                NetworkServiceUtil.Log("Socket GetWsData: finally");
                //  WifiAdapter.Instance.StopWebSocketConnection(client);
            }
            NetworkServiceUtil.Log("Socket GetWsData: end");
            return(null);
        }
Esempio n. 3
0
        public async Task <bool> ConnectToWifi(string ssid, string pwd)
        {
            m_ssid = ssid;
            m_pwd  = pwd;
            FormWifiManager.DisconnectWifi();
            var res = await FormWifiManager.Connect(ssid, pwd);

            NetworkServiceUtil.Log("Socket ConnectToWifi");
            return(res);
        }
Esempio n. 4
0
        public void Dispose()
        {
            if (_client != null)
            {
                _client.Dispose();
                _client = null;

                NetworkServiceUtil.Log("Socket Dispose");
            }
            //    wifiAdapter = null;
        }
Esempio n. 5
0
        internal async Task <ClientWebSocket> StartWebSocketConnection(string url)
        {
            //await Task.Delay(1000);
            ClientWebSocket client = null;

            try
            {
                NetworkServiceUtil.Log("Socket StartWebSocketConnection: " + url);
                client = new ClientWebSocket();
                await client.ConnectAsync(new Uri(url), CancellationToken.None);
            }
            catch (Exception e)
            {
                NetworkServiceUtil.Log("Socket StartWebSocketConnection Exception: " + e);
            }
            return(client);
        }
Esempio n. 6
0
        internal async Task <string> ReadMessage(ClientWebSocket client)
        {
            // Thread.Sleep(2100);
            NetworkServiceUtil.Log("Socket ReadMessage: start");
            try
            {
                NetworkServiceUtil.Log("Socket ReadMessage: 1");
                WebSocketReceiveResult result;
                string data = string.Empty;
                NetworkServiceUtil.Log("Socket ReadMessage: 2");
                // var rcvBytes = new byte[128];
                // var message = new ArraySegment<byte>(rcvBytes);
                var message = new ArraySegment <byte>(new byte[4096]);
                NetworkServiceUtil.Log("Socket ReadMessage: 3");
                bool IsIntiger;
                do
                {
                    var _result = client.ReceiveAsync(message, CancellationToken.None);
                    NetworkServiceUtil.Log("Socket ReadMessage: 3.1");
                    //   result = _result!=null? _result.Result:null;

                    result = _result.Result;

                    NetworkServiceUtil.Log("Socket ReadMessage: 4");
                    // if (result.MessageType != WebSocketMessageType.Text)
                    //   break;
                    var messageBytes = message.Skip(message.Offset).Take(result.Count).ToArray();
                    NetworkServiceUtil.Log("Socket ReadMessage: 5");
                    string receivedMessage = Encoding.UTF8.GetString(messageBytes);
                    NetworkServiceUtil.Log("Socket ReadMessage: 6");
                    data = receivedMessage;
                    NetworkServiceUtil.Log("Socket ReadMessage: 7");
                    NetworkServiceUtil.Log("Socket appendMsg: " + receivedMessage);
                    int a;
                    IsIntiger = int.TryParse(data, out a);
                }while (result != null && !result.EndOfMessage || IsIntiger || string.IsNullOrEmpty(data));
                NetworkServiceUtil.Log("Socket Received: " + data);
                return(data);
            }
            catch (Exception e)
            {
                NetworkServiceUtil.Log("Socket Received Exception: " + e);
                throw;
            }
        }
Esempio n. 7
0
 internal async Task <bool> SendMessageAsync(string message, ClientWebSocket client)
 {
     NetworkServiceUtil.Log("Socket SendMessageAsync: start");
     try
     {
         if (client != null)
         {
             if (client.State == WebSocketState.Open)
             {
                 var byteMessage = Encoding.UTF8.GetBytes(message);
                 var segmnet     = new ArraySegment <byte>(byteMessage);
                 NetworkServiceUtil.Log("Socket SendMessageAsync: " + message);
                 client.SendAsync(segmnet, WebSocketMessageType.Text, true, CancellationToken.None).Wait();
             }
         }
     }
     catch (Exception e)
     {
         NetworkServiceUtil.Log("Socket SendMessageAsync Exception: " + e);
     }
     NetworkServiceUtil.Log("Socket SendMessageAsync: end");
     return(true);
 }