Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 异步发送和接收telnet流时,while循环挂起telnet仿真器_Asp.net Mvc_Async Await_Signalr_Telnet - Fatal编程技术网

Asp.net mvc 异步发送和接收telnet流时,while循环挂起telnet仿真器

Asp.net mvc 异步发送和接收telnet流时,while循环挂起telnet仿真器,asp.net-mvc,async-await,signalr,telnet,Asp.net Mvc,Async Await,Signalr,Telnet,我正在使用mvc和signalR在web上开发telnet emulator,同时异步发送和接收telnet流,而循环挂起页面(如果长时间保存,则不会完成)。尝试了许多解决方案,但没有成功 下面是它在持续查找流字节时卡住的代码 /// <summary> /// Wait for data from the telnet server and send it to the emulation. /// </summary> public

我正在使用mvc和signalR在web上开发telnet emulator,同时异步发送和接收telnet流,而循环挂起页面(如果长时间保存,则不会完成)。尝试了许多解决方案,但没有成功

下面是它在持续查找流字节时卡住的代码

     /// <summary>
    /// Wait for data from the telnet server and send it to the emulation.
    /// </summary>
    public async Task ReadLoop(string connectionId, BaseDecoder decoder, CancellationToken ct, string PanelId)
    {
        var client = Get(connectionId);
        if (client == null) { return; }
        string script = string.Empty;
        if (string.IsNullOrWhiteSpace(panelScript))
        {
            panelScript = objAccn.ExecuteQueryPanelScript(Convert.ToInt32(PanelId)).ToString();
            script = panelScript.Replace(@"\n", Environment.NewLine);
            commands = Regex.Split(script, "\r\n");
        }

        string loginPrompt = null;
        if (PanelId != "1")

            loginPrompt = "login: ";
        else
            loginPrompt = "name?: ";
        var login = commands[0];// WebConfigurationManager.AppSettings["login"];
        if (!_panelCommands.ContainsKey(0))
            _panelCommands.Add(0, true);
        var passwordPrompt = WebConfigurationManager.AppSettings["passwordPrompt"];
        var password = commands[1];//WebConfigurationManager.AppSettings["password"];
        if (!_panelCommands.ContainsKey(1))
            _panelCommands.Add(1, true);
        var loginAuto = (!String.IsNullOrEmpty(loginPrompt) && !String.IsNullOrEmpty(login));
        var passwordAuto = (!String.IsNullOrEmpty(passwordPrompt) && !String.IsNullOrEmpty(password));
        var DefaultCommandsForm60 = false;
        var DefaultCommandsForm50 = false;
        var DefaultScreenm50 = false;
        decoder.ScriptFunc = async (string str) =>
            {
                if (!String.IsNullOrEmpty(str))
                {
                    if (loginAuto && str.EndsWith(loginPrompt, StringComparison.Ordinal))
                    {
                        await client.StreamWriter.WriteAsync(login + "\r\n");
                        loginAuto = false;
                        str = str.Remove(str.Length - loginPrompt.Length);
                    }
                    if (passwordAuto && str.EndsWith(passwordPrompt, StringComparison.Ordinal))
                    {
                        await client.StreamWriter.WriteAsync(password + "\r\n");
                        passwordAuto = false;
                        str = str.Remove(str.Length - passwordPrompt.Length);
                        if (PanelId != "1")
                            DefaultCommandsForm60 = true;
                        else
                            DefaultCommandsForm50 = true;
                        //System.Threading.Thread.Sleep(1500);
                    }
                    if (PanelId != "1")
                    {
                        if (DefaultCommandsForm60)
                        {
                            System.Threading.Thread.Sleep(1500);
                            await client.StreamWriter.WriteAsync(commands[2] + "\r\n");
                            if (commands.Length > 2)
                            {
                                System.Threading.Thread.Sleep(1500);
                                await client.StreamWriter.WriteAsync(commands[3] + "\r\n");
                            }
                            if (commands.Length > 3)
                            {
                                System.Threading.Thread.Sleep(1500);
                                await client.StreamWriter.WriteAsync(commands[4] + "\r\n");
                            }
                            DefaultCommandsForm60 = false;
                        }
                    }
                    else
                    {

                        if (DefaultCommandsForm50)
                        {
                            if (commands.Length > 1)
                            {
                                //  System.Threading.Thread.Sleep(2500);
                                if (!_panelCommands.ContainsKey(3))
                                {
                                    // System.Threading.Thread.Sleep(1500);
                                    await client.StreamWriter.WriteAsync(commands[3] + "\r\n");
                                    _panelCommands.Add(3, true);
                                }
                                else
                                {
                                    if (commands.Length > 2)
                                    {
                                        if (!_panelCommands.ContainsKey(4))
                                        {
                                            // System.Threading.Thread.Sleep(1500);
                                            await client.StreamWriter.WriteAsync(commands[3] + "\r\n");
                                            _panelCommands.Add(4, true);
                                        }
                                        DefaultCommandsForm50 = false;
                                    }
                                }
                                DefaultScreenm50 = true;
                            }
                        }
                        else
                        {
                            if (DefaultScreenm50)
                                if (str.EndsWith("$ "))
                                {
                                    await client.StreamWriter.WriteAsync("Screen" + "\r\n");
                                    str = str.Remove(str.Length - ("$ ").Length);
                                    DefaultScreenm50 = false;
                                }
                        }

                    }
                }
                return str;
            };

        const int bufferSize = 4096;
        //if (ns.CanRead)
        //{
        //    byte[] readBuffer = new byte[1024];
        //    int numBytesRead = 0;
        //    do
        //    {
        //        numBytesRead = ns.Read(readBuffer, 0, readBuffer.Length);
        //        //var data = Encoding.UTF8.GetString(readBuffer);
        //        // ss= Encoding.GetEncoding(1252).GetString(readBuffer.ToArray());
        //        //sb.Append(readBuffer[0].ToString);
        //        sb.AppendFormat("{0}", Encoding.ASCII.GetString(readBuffer, 0, numBytesRead));
        //        sb.Replace(Convert.ToChar(24), ' ');
        //        sb.Replace(Convert.ToChar(255), ' ');
        //        sb.Replace('?', ' ');
        //        //sb.Replace(Environment.NewLine, "<br />").ToString();
        //    }
        //    while (ns.DataAvailable);
        //}
        //if (client._stream.CanRead)
        //{
        //    do
        //    {
        //        var inBytes = await client.ReadAsync(bufferSize, ct);
        //        foreach (var b in inBytes)
        //        {
        //            await decoder.AddByte(b);
        //        }
        //        await decoder.Flush();
        //    } while (client.IsConnected );
        ////}
        //Disconnect(connectionId);
 //var  readTask = client.ReadAsync(bufferSize, ct);
        while (client.IsConnected && !ct.IsCancellationRequested)
        {
            if (client._stream.CanRead)
            {
                var inBytes = await client.ReadAsync(bufferSize, ct);
                foreach (var b in inBytes)
                {
                    await decoder.AddByte(b);
                }

                await decoder.Flush();
            }
        }
        Disconnect(connectionId);
    }
} 

任何建议和帮助都是值得赞赏的

信令器不使用telnet协议。它使用HTTP/WebSocket协议。SignalR用于保持服务器活动,代码隐藏连接tcp客户端?因此您的通信是通过原始tcp/IP套接字进行的,而不是SignalR?是的,可以这么说。但HUB正被用来通过telnet连接到IP@StephenCleary您认为我们是否可以克服这个问题,正如我尝试使用少量代码示例,但它不起作用。
while (client.IsConnected && !ct.IsCancellationRequested)
        {
            if (client._stream.CanRead)
            {
                var inBytes = await client.ReadAsync(bufferSize, ct);
                foreach (var b in inBytes)
                {
                    await decoder.AddByte(b);
                }

                await decoder.Flush();
            }
        }
        Disconnect(connectionId);