Security issues of Io operations in as3

Source: Internet
Author: User

As is used to compile the client in web games. Its Io operations mainly refer to network communication.

I encountered some problems in my project a few days ago.

There is no problem when testing the project on the local machine. After being uploaded to the internet server, the problem occasionally occurs.

The error code is similar to the following:

 

Buttom. addeventlistener (mouseevent. Click, prohandler); // This is not the real code in the project. You can give an example at will, so the variable name will compare 2

 

Function prohandler (Event: Event): void {

Socket. Send (data); // 1

Buttom. removeeventlistener (mouseevent. Click, prohandler); // 2

// Many other operations

//.......

}

 

Such code. Logically, there is no error.

However, problems may still occur.

 

When you click the buttom. The callback function prohandler is run.

The main operation of the prohandler function is socket. Send (data), which performs a network I/O operation.

 

The problem is that you can click buttom twice quickly. It may be a cup.

Because the first click, the callback function may run to socket. Send (data), and the second click calls the callback method.

This event is expected to have only one click. But now it has changed to two, and sent two data.

The server received the data twice. Errors may occur.

 

Therefore, we recommend that you write the prohandler method as follows.

Function prohandler (Event: Event): void {

Buttom. removeeventlistener (mouseevent. Click, prohandler); // 2

Socket. Send (data); // 1

// Many other operations

//.......

}

Change the execution order between 1 and 2. This is the case when you click twice.

The prohandler method removes the event from the beginning at 1.

This is the second event. Data is only sent once.

 

 

OK, turn these into a habit ..

 

Thank you for coming to the cold house.

If you have any errors or comments, please kindly advise

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.