A
A
Alexey Lebedev2012-11-16 19:56:54
ASP.NET
Alexey Lebedev, 2012-11-16 19:56:54

ASP.net server latency. Chat on long polling?

Previously, chat users sent a request to receive new messages once every half a second. I'd like to cut it all down and make one request every 25 seconds, when the chat is idle or as messages come in.

protected void Page_Load(object sender, EventArgs e)
    {
//... Проверка авторизации и прочее...
        using (var conn = new SqlConnection(ConfigurationManager.AppSettings["sql"]))
        {
            conn.Open();
            Response.Write(@"{""chat"":[");
             // Один запрос к БД. Выполняется 1 раз
             // ..................


           // Запрос к БД для получении новых сообщений. Его надо делать раз в пол секунды.
            for(byte i=0; i<50; i++)
           {
                using (var comm = new SqlCommand("...", conn, null))
                {

                    using (var reader = comm.ExecuteReader())
                    {
                        byte i = 0;
                        while (reader.Read())
                        {
                            if (i != 0)
                            {
                                Response.Write(@",");

                            }
                            i++;
                           //Вывод результатов.
                       }
                   }
                 Thread.Sleep(500);
               }
            }
            Response.Write(@"]}");
        }
    }
}

Such a request slows down the entire server. I heard about streams, but it seems to me that this can be solved in a simple way.
What is the best thing to do in this case?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Guketlev, 2012-11-16
@swanrnd

a) don't hammer the base every 500 milliseconds. If you have 100 people in a chat, you get 200 database requests per second. Aren't you afraid that the base will sag? Make a static class that will store your chat. In fact, it will need 2 methods: add a message (added to the internal storage and to the database) and receive messages (from the internal storage). In the static constructor, when the application starts, you load the latest messages into memory. Keep in mind that you need to do a thread-safe class.
b) Lochte processing threads on Monitor, and when a message arrives, reset it from class statics. (see here: stackoverflow.com/questions/12522714/thread-syncronization-with-eventhandler )
c) I think Asynchronous Controllers from MVC are ideal for you here. Not sure if it's possible in ASP.NET to send a thread to the background to free up the processing thread.
d) it is not “the whole server” that slows down, but a thread from the request processing thread pool. Although if you have only 1 thread in the pool, then yes - the entire server
e) before taking on this task, I recommend dealing with threads more tightly than “I heard about”. All the same, .NET is not php, and here the entry level is higher, although it may seem that everything is easy here.
e) I understand when people write to a rova ​​or with it , well, as I hear it, I write it. But what, tell me, what makes you write "in this case and "? It's even pronounced with an e.

L
lam0x86, 2012-11-19
@lam0x86

I advise you to read about the SignalR library .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question