private void _WebSocket_OnMessage(object sender, MessageEventArgs e)
        {
            _lastReceivedTime = DateTime.UtcNow;

            if (e.IsBinary)
            {
                string data = GZipDecompresser.Decompress(e.RawData);

                var pingMessage = JsonConvert.DeserializeObject <PingMessageV1>(data);
                if (pingMessage.IsPing())
                {
                    AppLogger.Trace($"WebSocket received data, ping={pingMessage.ts}");
                    string pongData = $"{{\"op\":\"pong\", \"ts\":{pingMessage.ts}}}";
                    _WebSocket.Send(pongData);
                    AppLogger.Trace($"WebSocket repied data, pong={pingMessage.ts}");
                }
                else
                {
                    dynamic json = JToken.Parse(data);
                    string  op   = json.op;
                    if (String.Equals(op, "auth"))
                    {
                        var response = JsonConvert.DeserializeObject <WebSocketV1AuthResponse>(data);

                        OnAuthenticationReceived?.Invoke(response);
                    }
                    else
                    {
                        var response = JsonConvert.DeserializeObject <DataResponseType>(data);

                        OnDataReceived?.Invoke(response);
                    }
                }
            }
        }
Beispiel #2
0
        private void _WebSocket_OnMessage(object sender, MessageEventArgs e)
        {
            _lastReceivedTime = DateTime.UtcNow;

            if (e.IsBinary)
            {
                string data = GZipDecompresser.Decompress(e.RawData);

                var pingMessage = JsonConvert.DeserializeObject <PingMessageV1>(data);
                if (pingMessage.IsPing())
                {
                    Console.WriteLine($"Received ping:{pingMessage.ts}");
                    string pongData = $"{{\"op\":\"pong\", \"ts\":{pingMessage.ts}}}";
                    _WebSocket.Send(pongData);
                    Console.WriteLine($"Replied pong:{pingMessage.ts}");
                }
                else
                {
                    JToken json = JToken.Parse(data);
                    string op   = json.Value <string>("op");
                    if (String.Equals(op, "auth"))
                    {
                        var response = JsonConvert.DeserializeObject <WebSocketV1AuthResponse>(data);

                        OnAuthenticationReceived?.Invoke(response);
                    }
                    else
                    {
                        var response = JsonConvert.DeserializeObject <DataResponseType>(data);

                        OnDataReceived?.Invoke(response);
                    }
                }
            }
        }
Beispiel #3
0
        private void _WebSocket_OnMessage(object sender, MessageEventArgs e)
        {
            _lastReceivedTime = DateTime.UtcNow;

            string data = e.Data;

            if (e.IsBinary)
            {
                data = GZipDecompresser.Decompress(e.RawData);
            }

            dynamic json = JToken.Parse(data);
            string  ch   = json.action;

            switch (ch)
            {
            case "ping":     // Receive Ping message
            {
                var pingMessageV2 = JsonConvert.DeserializeObject <PingMessageV2>(data);
                if (pingMessageV2 != null && pingMessageV2.data != null && pingMessageV2.data.ts != 0)
                {
                    long ts = pingMessageV2.data.ts;

                    //_logger.Log(Log.LogLevel.Trace, $"WebSocket received data, ping={ts}");

                    string pongData = $"{{\"action\": \"pong\", \"data\": {{\"ts\":{ts} }} }}";
                    _WebSocket.Send(pongData);

                    //_logger.Log(Log.LogLevel.Trace, $"WebSocket replied data, pong={ts}");
                }
                break;
            }

            case "req":     // Receive authentication response
            {
                var response = JsonConvert.DeserializeObject <WebSocketV2AuthResponse>(data);

                OnAuthenticationReceived?.Invoke(response);

                break;
            }

            case "sub":     // Receive subscription response
            case "push":    // Receive data response
            {
                var response = JsonConvert.DeserializeObject <DataResponseType>(data);

                OnDataReceived?.Invoke(response);

                break;
            }
            }
        }