Game Hall starting from the Basics (6)--around the back of the chat room (middle) maid Chronicle 1

Source: Internet
Author: User

We have a general understanding of the behavior patterns of several chat rooms

The simplest and most straightforward push mode requires almost no extra language to describe its implementation.

This article we see how to achieve the pull mode is more effective.

This diagram clearly shows the behavior of the "pull" mode chat room.

Concurrent multiuser writes data to data pool

Concurrent multiuser reading from data pool

Data is best stored in the collection in chronological order

An enumeration lookup that is backward at some time will be the most consumed.

Chat room Evolution-Maid chronicle of the Mysterious Primitive Society

Still refer to our magical simplicity of Asp3 chat room

53     Application.lock
54     Application("show5")=Application("show4") '一条新信息 驾到 第五条信息被淘汰
55     Application("show4")=Application("show3")
56     Application("show3")=Application("show2")
57     Application("show2")=Application("show")
58     Application("show")=NewMessage     '其他所有的信息向前移 动一次给新的信息让个位置。
59     Application.UnLock
60     Response.Write Application ("show5")
61     Response.Write Application("show4") '由于 是postback 模式 必须输出历史n行数据
62     Response.Write Application ("show3")
63     Response.Write Application("show2")
64      Response.Write Application("show")

From a thread-safe standpoint, Response.Write should also be in the application. Lock block or separate two lock blocks. But here, due to the delay in Response.Write mode, the authors have painstakingly moved them out of the security lock. There is a good chance of missing or repeating speeches in actual operation.

What the hell did application do? No boundaries, no abstract packaging. This realization is like the original communism who is who who ah this is all!

Private ownership appears, slave society lock~ This slave is mine ~

Translated into C # we can see a more understandable logic of course, this code is slightly modified. The two locks are clear and perfect. Line up the data and threads

Code Snippet

class Channel
{
 Queue<string> MessageQ = new Queue<string>();

 public void Say(string message) //写信息
 {
  lock (MessageQ)
  {
   MessageQ.Enqueue(message);
   while (MessageQ.Count > 5)  // 删多余
   {
    MessageQ.Dequeue();
   }

  }
 }

 public string[] Listen() //u-28781 ?出所有
 {
  lock (MessageQ)
  {
   return MessageQ.ToArray();

  }

 }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.