예제 #1
0
        public async Task ProcessRequest(AspNetWebSocketContext context)
        {
            var socket = context.WebSocket; //传入的context中有当前的web socket对象

            _sockets.Add(socket);           //此处将web socket对象加入一个静态列表中

            string connectionId = context.AnonymousID;
            //string code = context.QueryString["code"];
            string strategy_id = context.QueryString["strategy_id"];
            string time        = context.QueryString["time"];
            string user_id     = context.QueryString["user_id"];

            // backtest item
            int    data_id     = 1;
            var    sy          = "";
            var    nsy         = "";
            var    hc          = "";
            var    xp          = "";
            string report_path = "";



            DateTime dt = DateTime.ParseExact(time, "yyyy-MM-dd HH:mm:ss",
                                              System.Globalization.CultureInfo.InvariantCulture);
            string new_time = dt.ToString("yyyyMMddHHmmss");

            string        origin_time = dt.ToString("yyyy-MM-dd HH:mm:ss");
            StringBuilder sb          = new StringBuilder();

            Convert(origin_time, sb);
            string new_time_2 = sb.ToString();


            string path = common_path + @"\" + user_id;
            string file = new_time + ".py";

            //utils.saveFile(code, path, file);
            utils.copyFile(path + @"\" + file, common_path + @"\" + file);



            // 回测


            Task t = new Task(() => {
                string sArguments   = common_path + @"\" + file;
                EngineRunner runner = new EngineRunner(user_id);
                runner.RunPythonScript(sArguments);
            });

            t.Start();

            byte[] bytes;
            ArraySegment <byte> buffer;


            var topic = user_id;

            using (var subSocket = new SubscriberSocket()) {
                subSocket.Options.ReceiveHighWatermark = 1000;
                subSocket.Connect("tcp://*****:*****@"\" + backtest_id + @".docx";
                WordFileOperator.CreateReport(code, time, file_path);


                //进入一个无限循环,当web socket close时循环结束
                while (true)
                {
                    //var buffer = new ArraySegment<byte>(new byte[1024]);
                    var receivedResult = await socket.ReceiveAsync(buffer, CancellationToken.None);//对web socket进行异步接收数据

                    if (receivedResult.MessageType == WebSocketMessageType.Close)
                    {
                        await socket.CloseAsync(WebSocketCloseStatus.Empty, string.Empty, CancellationToken.None);//如果client发起close请求,对client进行ack

                        _sockets.Remove(socket);
                        break;
                    }

                    if (socket.State == System.Net.WebSockets.WebSocketState.Open)
                    {
                        string recvMsg    = Encoding.UTF8.GetString(buffer.Array, 0, receivedResult.Count);
                        var    recvBytes  = Encoding.UTF8.GetBytes(recvMsg);
                        var    sendBuffer = new ArraySegment <byte>(recvBytes);
                        foreach (var innerSocket in _sockets)//当接收到文本消息时,对当前服务器上所有web socket连接进行广播
                        {
                            if (innerSocket != socket)
                            {
                                await innerSocket.SendAsync(sendBuffer, WebSocketMessageType.Text, true, CancellationToken.None);
                            }
                        }
                    }
                }
            }
        }