Ejemplo n.º 1
0
        public async Task Invoke(HttpContext context)
        {
            if (!context.WebSockets.IsWebSocketRequest)
            {
                await _next.Invoke(context);

                return;
            }

            /*string strSecWebSocketKeyValue = context.Request.Headers["Sec-WebSocket-Key"].ToString();
             * SHA1 sha = SHA1.Create();
             * string strSecWebSocketKeyValue_ForSha = strSecWebSocketKeyValue + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
             * byte[] hash_buffer = Encoding.Default.GetBytes(strSecWebSocketKeyValue_ForSha);
             * byte[] hash_result = sha.ComputeHash(hash_buffer);
             * string strResult = Encoding.Default.GetString(hash_result);
             * string strResultBase64 = Util_Common.Encoder_Base64(strResult);
             * if (!context.Response.Headers.ContainsKey("Sec-WebSocket-Accept"))
             *      context.Response.Headers.Add("Sec-WebSocket-Accept", strResultBase64);
             * else
             *      context.Response.Headers["Sec-WebSocket-Accept"] = strResultBase64;
             * context.Response.Headers["Sec-WebSocket-Protocol"] = "chat";
             */

            string token = get_ClientToken(context.Request, "student_token");

            if (Global.LoginServices.verify_logined_token(token))
            {
                CancellationToken ct = context.RequestAborted;
                var currentSocket    = await context.WebSockets.AcceptWebSocketAsync();

                AppLoader newApploader = new AppLoader();
                string    uname        = Global.LoginServices.Pool_Logined[token].name;
                _accountTokenMap.TryAdd(uname, token);
                string socketId = token;
                if (!_sockets.ContainsKey(socketId))
                {
                    _sockets.TryAdd(socketId, currentSocket);
                    newApploader.InitApiConfigs(Global.GlobalDefines.SY_CONFIG_FILE);
                    newApploader.ConnectDB(Global.GlobalDefines.DB_KEY_IKCODER_BASIC);
                    newApploader.LoadSPS(Global.GlobalDefines.DB_SPSMAP_FILE);
                }

                while (true)
                {
                    try
                    {
                        if (ct.IsCancellationRequested)
                        {
                            break;
                        }

                        string response = await ReceiveStringAsync(currentSocket, ct);

                        if (string.IsNullOrEmpty(response))
                        {
                            if (currentSocket.State != WebSocketState.Open)
                            {
                                break;
                            }
                            continue;
                        }
                        string activeAction  = string.Empty;
                        string returnContent = ProcessProtocal(token, response, newApploader, out activeAction);
                        await SendStringAsync(currentSocket, BuildReturnDoc(returnContent, activeAction, false), ct);
                    }
                    catch
                    {
                        continue;
                    }
                }
                WebSocket dummy_socket;
                _sockets.TryRemove(socketId, out dummy_socket);
                string dummy_token;
                _accountTokenMap.TryRemove(uname, out dummy_token);
                newApploader.CloseDB();
                await currentSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", ct);

                currentSocket.Dispose();
            }
        }