示例#1
0
        /// <summary>
        /// reply msg handle
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _WebSocket_OnMessage(object sender, MessageEventArgs e)
        {
            string data = null;

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

            JObject jdata = JObject.Parse(data);

            if (jdata.ContainsKey("ping"))// market heartbeat
            {
                long ts = jdata["ping"].ToObject <long>();

                //_logger.Log(Log.LogLevel.Debug, $"websocket has received data: {data}");

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

                //_logger.Log(Log.LogLevel.Debug, $"websocket has send data: {pongData}");
            }
            else if (jdata.ContainsKey("op"))
            {
                string op = jdata["op"].ToObject <string>();
                switch (op)
                {
                case "ping":     // order heartbeat
                    string ts = jdata["ts"].ToObject <string>();
                    //_logger.Log(Log.LogLevel.Debug, $"websocket has received data: {data}");

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

                    //_logger.Log(Log.LogLevel.Debug, $"websocket has send data: {pongData}");
                    break;

                case "close":
                    _logger.Log(Log.LogLevel.Error, $"websocket has received data: {data}");
                    break;

                case "error":
                    _logger.Log(Log.LogLevel.Trace, $"websocket has received data: {data}");
                    break;

                case "auth":
                    _logger.Log(Log.LogLevel.Info, $"websocket has received data: {data}");
                    int code = jdata["err-code"].ToObject <int>();
                    if (code == 0)
                    {
                        _canWork = true;
                    }
                    break;

                case "notify":
                    _HandleSubCallbackFun(jdata["topic"].ToObject <string>(), data, jdata);
                    break;

                case "sub":
                    _logger.Log(Log.LogLevel.Info, $"websocket has received data: {data}");
                    break;

                case "unsub":
                    _logger.Log(Log.LogLevel.Info, $"websocket has received data: {data}");
                    string ch = jdata["topic"].ToObject <string>();
                    if (_onSubCallbackFuns.ContainsKey(ch))
                    {
                        _onSubCallbackFuns.Remove(ch);
                    }
                    break;

                default:
                    _logger.Log(Log.LogLevel.Info, $"unknown data: {data}");
                    break;
                }
            }
            else if (jdata.ContainsKey("subbed")) // sub success reply
            {
                _logger.Log(Log.LogLevel.Info, $"websocket has received data: {data}");
            }
            else if (jdata.ContainsKey("unsubbed")) // unsub success reply
            {
                _logger.Log(Log.LogLevel.Info, $"websocket has received data: {data}");
                string ch = jdata["unsubbed"].ToObject <string>();
                if (_onSubCallbackFuns.ContainsKey(ch))
                {
                    _onSubCallbackFuns.Remove(ch);
                }
            }
            else if (jdata.ContainsKey("ch")) // market sub reply data
            {
                _HandleSubCallbackFun(jdata["ch"].ToObject <string>(), data, jdata);
            }
            else if (jdata.ContainsKey("rep")) // market request reply data
            {
                _HandleReqCallbackFun(jdata["rep"].ToObject <string>(), data, jdata);
            }
            else if (jdata.ContainsKey("err-code")) // market request reply data
            {
                _logger.Log(Log.LogLevel.Info, $"websocket has received data: {data}");
            }
            else
            {
                _logger.Log(Log.LogLevel.Info, $"unknown data: {data}");
            }
        }
示例#2
0
        /// <summary>
        /// reply msg handle
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnMsg(object sender, MessageEventArgs e)
        {
            string data = null;

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

            JObject jdata = JObject.Parse(data);

            if (jdata.ContainsKey("ping"))// market heartbeat
            {
                long ts = jdata["ping"].ToObject <long>();

                //logger.Log(Log.LogLevel.Debug, $"websocket has received data: {data}");

                string pongData = $"{{\"pong\":{ts} }}";
                websocket.Send(pongData);

                //logger.Log(Log.LogLevel.Debug, $"websocket has send data: {pongData}");
            }
            else if (jdata.ContainsKey("action"))// spot order heartbeat
            {
                string action = jdata["action"].ToObject <string>();
                switch (action)
                {
                case "ping":     // order heartbeat
                    string ts = jdata["data"]["ts"].ToObject <string>();
                    //logger.Log(Log.LogLevel.Debug, $"websocket has received data: {data}");

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

                    //logger.Log(Log.LogLevel.Debug, $"websocket has send data: {pongData}");
                    break;

                case "push":
                    if (callbackFun != null)
                    {
                        callbackFun.DynamicInvoke(JsonConvert.DeserializeObject(data, paramType));
                    }
                    else
                    {
                        logger.Log(Log.LogLevel.Info, $"websocket has received data: {data}");
                    }
                    break;

                case "req":
                case "sub":
                case "unsub":
                    logger.Log(Log.LogLevel.Info, $"websocket has received data: {data}");
                    break;

                default:
                    logger.Log(Log.LogLevel.Info, $"unknown data: {data}");
                    break;
                }
            }
            else if (jdata.ContainsKey("op"))
            {
                string op = jdata["op"].ToObject <string>();
                switch (op)
                {
                case "ping":     // order heartbeat
                    string ts = jdata["ts"].ToObject <string>();
                    //logger.Log(Log.LogLevel.Debug, $"websocket has received data: {data}");

                    string pongData = $"{{ \"op\":\"pong\", \"ts\":{ts} }}";
                    websocket.Send(pongData);

                    //logger.Log(Log.LogLevel.Debug, $"websocket has send data: {pongData}");
                    break;

                case "notify":
                    if (callbackFun != null)
                    {
                        callbackFun.DynamicInvoke(JsonConvert.DeserializeObject(data, paramType));
                    }
                    else
                    {
                        logger.Log(Log.LogLevel.Info, $"websocket has received data: {data}");
                    }
                    break;

                case "close":
                case "error":
                case "auth":
                case "sub":
                case "unsub":
                    logger.Log(Log.LogLevel.Info, $"websocket has received data: {data}");
                    break;

                default:
                    logger.Log(Log.LogLevel.Info, $"unknown data: {data}");
                    break;
                }
            }
            else if (jdata.ContainsKey("subbed")) // sub success reply
            {
                logger.Log(Log.LogLevel.Info, $"websocket has received data: {data}");
            }
            else if (jdata.ContainsKey("unsubbed")) // unsub success reply
            {
                logger.Log(Log.LogLevel.Info, $"websocket has received data: {data}");
            }
            else if (jdata.ContainsKey("ch")) // market sub reply data
            {
                if (callbackFun != null)
                {
                    callbackFun.DynamicInvoke(JsonConvert.DeserializeObject(data, paramType));
                }
                else
                {
                    logger.Log(Log.LogLevel.Info, $"websocket has received data: {data}");
                }
            }
            else if (jdata.ContainsKey("rep")) // market request reply data
            {
                if (callbackFun != null)
                {
                    callbackFun.DynamicInvoke(JsonConvert.DeserializeObject(data, paramType));
                }
                else
                {
                    logger.Log(Log.LogLevel.Info, $"websocket has received data: {data}");
                }
            }
            else if (jdata.ContainsKey("err-code")) // market request reply data
            {
                logger.Log(Log.LogLevel.Info, $"websocket has received data: {data}");
            }
            else
            {
                logger.Log(Log.LogLevel.Info, $"unknown data: {data}");
            }
        }