ThreadPool.QueUeUserWorkITEMを使用してTCPCLIENT接続を開き、ASP.NETおよびSIGNALRでデータを読み取る

StackOverflow https://stackoverflow.com/questions/8373081

質問

SignRRに関するいくつかの投稿を読み、楽しいテストプロジェクトを考えて、Webアプリケーションを作成してステータスのためにOnkyoレシーバーを投票し、ブラウザーに結果を表示できると考えました。最初のテストのために、Application_startでこのコードを使用して、サーバー上の現在の時間をクライアントに返送することができました。

ThreadPool.QueueUserWorkItem(_ =>
{               
    dynamic clients = Hub.GetClients<KudzuHub>();
    while (true)
    {                
        clients.addMessage(DateTime.Now.ToString());

        Thread.Sleep(1000);
    }
});   

クライアントJavaScriptには、次のコードがあります。

// Proxy created on the fly
var kHub = $.connection.kudzuHub;

// Declare a function on the hub so that the server can invoke it
kHub.addMessage = function (message) {
    console.log('message added');
    $('#messages').append('<li>' + message + '</li>');
};

// start the connection
$.connection.hub.start();

そのため、すべてが正常に機能します。毎秒、現在のサーバーの日付と時刻を含む新しいリスト項目を取得します。

このコードをOnkyoレシーバーからデータを読むためにこのコードを追加すると、それは壊れます:(まだapplication_startに)

ThreadPool.QueueUserWorkItem(_ =>
{  
    dynamic clients = Hub.GetClients<KudzuHub>();
    try
    {
        while (true)
        {
            string host = ConfigurationManager.AppSettings["receiverIP"].ToString();
            int port = Convert.ToInt32(ConfigurationManager.AppSettings["receiverPort"]);

            TcpClient tcpClient = new TcpClient(host, port);

            NetworkStream clientSockStream = tcpClient.GetStream();

            byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
            clientSockStream.Read(bytes, 0, (int)tcpClient.ReceiveBufferSize);

            tcpClient.Close();

            clients.addMessage(System.Text.Encoding.ASCII.GetString(bytes));
            Thread.Sleep(50);
        }
    }
    catch (SocketException ex)
    {
        // do something to handle the error
    }

});

ブレークポイントを設定し、コードを踏み出しました。この行に到達してから戻ります。

clientSockStream.Read(bytes, 0, (int)tcpClient.ReceiveBufferSize);

クライアントにメッセージを送信するために残りのコードが終了することはありません。私は何が間違っているのですか?

ありがとう。

役に立ちましたか?

解決

ループにいくつかの構造的な変更を加えて、受信機の時間を応答し、50ミリ秒ごとに構成を取得するオーバーヘッドを削除し、オープンネットワークストリームをクリーンアップします。

ThreadPool.QueueUserWorkItem(_ =>
{  
    dynamic clients = Hub.GetClients<KudzuHub>();
    TcpClient tcpClient = null;
    NetworkStream clientSockStream = null;

    try
    {
        string host = ConfigurationManager.AppSettings["receiverIP"].ToString();
        int port = Convert.ToInt32(ConfigurationManager.AppSettings["receiverPort"]);

        while (true)
        {
            if (tcpClient == null) {
              tcpClient = new TcpClient(host, port);
              clientSockStream = tcpClient.GetStream();
            }

            if (clientSockStream.CanRead) {
                byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
                try {
                   clientSockStream.Read(bytes, 0, (int)tcpClient.ReceiveBufferSize);
                } catch (Exception ex) {
                  // Add some debug code here to examine the exception that is thrown
                }

                tcpClient.Close();
                // Closing the client does not automatically close the stream
                clientSockStream.Close();

                tcpClient = null;
                clientSockStream = null;

                clients.addMessage(System.Text.Encoding.ASCII.GetString(bytes));
            }
            Thread.Sleep(50);
        }
    }
    catch (SocketException ex)
    {
        // do something to handle the error
    } finally {
       if (tcpClient != null) {
         tcpClient.Close();
         clientSockStream.Close();
       }
    } 

});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top