Exemplo n.º 1
0
        private async void ConectWebSocket()
        {
            ClientWebSocket wss = new ClientWebSocket();

            wss.Options.Cookies = Instance.Auth;
            foreach (Cookie c in Instance.Auth.GetCookies(new Uri(domain)))
            {
                if (c.Name == "BPMCSRF" && !String.IsNullOrEmpty(c.Value))
                {
                    wss.Options.SetRequestHeader(c.Name, c.Value);
                    wss.Options.KeepAliveInterval = TimeSpan.FromSeconds(60);
                }
            }

            string socketDomain;

            if (domain.StartsWith("https://", StringComparison.Ordinal))
            {
                socketDomain = domain.Replace("https://", "wss://");
            }
            else
            {
                socketDomain = domain.Replace("http://", "ws://");
            }

            await wss.ConnectAsync(new Uri($"{socketDomain}/0/Nui/ViewModule.aspx.ashx"), CancellationToken.None).ConfigureAwait(false);

            while (wss.State == WebSocketState.Open)
            {
                ArraySegment <Byte>    buffer = new ArraySegment <byte>(new Byte[8192]);
                WebSocketReceiveResult result;
                using (var ms = new MemoryStream())
                {
                    do
                    {
                        result = await wss.ReceiveAsync(buffer, CancellationToken.None).ConfigureAwait(false);

                        ms.Write(buffer.Array, buffer.Offset, result.Count);
                    }while (!result.EndOfMessage);

                    ms.Seek(0, SeekOrigin.Begin);

                    if (result.MessageType == WebSocketMessageType.Text)
                    {
                        using (var reader = new StreamReader(ms, Encoding.UTF8))
                        {
                            string txt = reader.ReadToEnd();
                            WebSocketMessageReceivedEventArgs e = JsonConvert.DeserializeObject <WebSocketMessageReceivedEventArgs>(txt);
                            Instance.OnWebSocketMessageReceived(e);
                        }
                    }
                }
            }
            wss.Dispose();
        }
Exemplo n.º 2
0
 private static void WebSocketMessageReceived(object sender, WebSocketMessageReceivedEventArgs e)
 {
     Console.WriteLine();
     Console.ForegroundColor = ConsoleColor.Red;
     Console.WriteLine("---------------- New WebSocket Message ----------------");
     Console.WriteLine($"Id:\t\t{e.MessageId}");
     Console.WriteLine($"Header:\t\t{e.MessageHeader}");
     Console.WriteLine($"Body:\t\t{e.MessageBody}");
     Console.WriteLine("-------------------------------------------------------");
     Console.ResetColor();
 }
Exemplo n.º 3
0
        private void OnWebSocketMessageReceived(WebSocketMessageReceivedEventArgs e)
        {
            EventHandler <WebSocketMessageReceivedEventArgs> handler = WebSocketMessageReceived;

            handler?.Invoke(this, e);
        }