Design of Flash chat based on HTTP (real-time communication)

Source: Internet
Author: User
Tags character set json connect php code sleep socket usleep vars

The first two years have been a socket based flash chat code, but not all cases will choose (or have the conditions) to use the socket of the server. Real-time communication based on HTTP can be used in programs that are not too high on the demand for immediacy.

The principle is to obtain information through the client request, if there is no need for content, the background program will wait for a period of time (reaction speed), and then see if there is a need for results. Returns timeout information if no results are run more than once. Let the client connect again.

$times
				=
				0;
function Read ($id)
{
	$link
				= getmysq ();
	$rs
				=
				mysql_query ("Select Id,msg,t from C where id>". $id);
	Returns the result $result the JSON format
				=
				"[";
	$ns
				=
				true;
	while ($row
				=
				mysql_fetch_object ($rs))
	{
		$result
				=
				$result. " {"M": "". $row->msg. "", "." " I ":" "$row->id." "},";
		$ns
				=
				false;
	}
	$result
				=
				$result. " {}]";
	if ($ns)
	{
		mysql_close ($link);
		$times + +;
		5 requests also did not respond, do timeout judgment, let the client request
		if ($times
				>=
				5)
		{	
			echo
				"[]";
			return;
		}
		If there is currently no readable data, wait 1 seconds to read again, if it takes a shorter time (this is quicker), use Usleep, the unit is millisecond sleep
		(1);
		Read ($id);
	}
	else
	{
		echo
				$result;
		Mysql_close ($link);
	}

As code:

//http://localhost/ajax/chat.php?type=r&id=1 var lv:loadvars = new
Loadvars ();
Lv.load ("http://localhost/ajax/chat.php?type=r&id=2");
          Lv.ondata = function (d) {trace (d) var maxid = 0;
	JSON please go to json.org to download var data = (new JSON ()). Parse (d);
	str = "";
		for (var i=0;i<data.length-1;i++) {str + = data[i].m+ ' \ n ';
	Maxid = data[i].i;
	} M.text + = str;
	M.scroll = M.maxscroll;
Lv.load ("http://localhost/ajax/chat.php?type=r&id=" +MAXID);
}///Key.addlistener (this);
	OnKeyDown = function () {if (Key.getascii () = () {if (selection.getfocus () = = "_level0.smsg") sendmsg ();
				}     send.onpress = function () {sendmsg ();}   function sendmsg () {if (Smsg.text.length >
		1) {var slv:loadvars = new Loadvars ();
		Slv.load ("http://localhost/ajax/chat.php?msg=" +smsg.text);
	Smsg.text = ""; }
}

Flash source file Download:http://roading.net/res/blog/pic/httpchat.fla

Complete PHP Code:

<?php/* Establish a chat database to create a table:----Table structure ' C '--CREATE table ' C ' (' ID ' int (8) Not NULL auto_increment, ' msg ' var
char (character set UTF8 default null, ' t ' smallint (2) Not NULL default ' 0 ', PRIMARY KEY (' id ')); * * $type =

The first two years have been a socket based flash chat code, but not all cases will choose (or have the conditions) to use the socket of the server. Real-time communication based on HTTP can be used in programs that are not too high on the demand for immediacy.

The principle is to obtain information through the client request, if there is no need for content, the background program will wait for a period of time (reaction speed), and then see if there is a need for results. Returns timeout information if no results are run more than once. Let the client connect again.

$times
								=
								0;
function Read ($id)
{
	$link
								= getmysq ();
	$rs
								=
								mysql_query ("Select Id,msg,t from C where id>". $id);
	Returns the result $result the JSON format
								=
								"[";
	$ns
								=
								true;
	while ($row
								=
								mysql_fetch_object ($rs))
	{
		$result
								=
								$result. " {"M": "". $row->msg. "", "." " I ":" "$row->id." "},";
		$ns
								=
								false;
	}
	$result
								=
								$result. " {}]";
	if ($ns)
	{
		mysql_close ($link);
		$times + +;
		5 requests also did not respond, do timeout judgment, let the client request
		if ($times
								>=
								5)
		{	
			echo
								"[]";
			return;
		}
		If there is currently no readable data, wait 1 seconds to read again, if it takes a shorter time (this is quicker), use Usleep, the unit is millisecond sleep
		(1);
		Read ($id);
	}
	else
	{
		echo
								$result;
		Mysql_close ($link);
	}

As code:

//http://localhost/ajax/chat.php?type=r&id=1 var lv:loadvars = new Load
Vars ();
Lv.load ("http://localhost/ajax/chat.php?type=r&id=2");
          Lv.ondata = function (d) {trace (d) var maxid = 0;
	JSON please go to json.org to download var data = (new JSON ()). Parse (d);
	str = "";
		for (var i=0;i<data.length-1;i++) {str + = data[i].m+ ' \ n ';
	Maxid = data[i].i;
	} M.text + = str;
	M.scroll = M.maxscroll;
Lv.load ("http://localhost/ajax/chat.php?type=r&id=" +MAXID);
}///Key.addlistener (this);
	OnKeyDown = function () {if (Key.getascii () = () {if (selection.getfocus () = = "_level0.smsg") sendmsg ();
								}     send.onpress = function () {sendmsg ();}   function sendmsg () {if (Smsg.text.length >
		1) {var slv:loadvars = new Loadvars ();
		Slv.load ("http://localhost/ajax/chat.php?msg=" +smsg.text);
	Smsg.text = ""; }
}

Flash source file Download:http://roading.net/res/blog/pic/httpchat.fla

Complete PHP Code:

___fckpd___2
get["type"]; If

The first two years have been a socket based flash chat code, but not all cases will choose (or have the conditions) to use the socket of the server. Real-time communication based on HTTP can be used in programs that are not too high on the demand for immediacy.

The principle is to obtain information through the client request, if there is no need for content, the background program will wait for a period of time (reaction speed), and then see if there is a need for results. Returns timeout information if no results are run more than once. Let the client connect again.

$times
								=
								0;
function Read ($id)
{
	$link
								= getmysq ();
	$rs
								=
								mysql_query ("Select Id,msg,t from C where id>". $id);
	Returns the result $result the JSON format
								=
								"[";
	$ns
								=
								true;
	while ($row
								=
								mysql_fetch_object ($rs))
	{
		$result
								=
								$result. " {"M": "". $row->msg. "", "." " I ":" "$row->id." "},";
		$ns
								=
								false;
	}
	$result
								=
								$result. " {}]";
	if ($ns)
	{
		mysql_close ($link);
		$times + +;
		5 requests also did not respond, do timeout judgment, let the client request
		if ($times
								>=
								5)
		{	
			echo
								"[]";
			return;
		}
		If there is currently no readable data, wait 1 seconds to read again, if it takes a shorter time (this is quicker), use Usleep, the unit is millisecond sleep
		(1);
		Read ($id);
	}
	else
	{
		echo
								$result;
		Mysql_close ($link);
	}

As code:

//http://localhost/ajax/chat.php?type=r&id=1 var lv:loadvars = new Load
Vars ();
Lv.load ("http://localhost/ajax/chat.php?type=r&id=2");
          Lv.ondata = function (d) {trace (d) var maxid = 0;
	JSON please go to json.org to download var data = (new JSON ()). Parse (d);
	str = "";
		for (var i=0;i<data.length-1;i++) {str + = data[i].m+ ' \ n ';
	Maxid = data[i].i;
	} M.text + = str;
	M.scroll = M.maxscroll;
Lv.load ("http://localhost/ajax/chat.php?type=r&id=" +MAXID);
}///Key.addlistener (this);
	OnKeyDown = function () {if (Key.getascii () = () {if (selection.getfocus () = = "_level0.smsg") sendmsg ();
								}     send.onpress = function () {sendmsg ();}   function sendmsg () {if (Smsg.text.length >
		1) {var slv:loadvars = new Loadvars ();
		Slv.load ("http://localhost/ajax/chat.php?msg=" +smsg.text);
	Smsg.text = ""; }
}

Flash source file Download:http://roading.net/res/blog/pic/httpchat.fla

Complete PHP Code:

___fckpd___2
get["type" = = = "R") {$id =

The first two years have been a socket based flash chat code, but not all cases will choose (or have the conditions) to use the socket of the server. Real-time communication based on HTTP can be used in programs that are not too high on the demand for immediacy.

The principle is to obtain information through the client request, if there is no need for content, the background program will wait for a period of time (reaction speed), and then see if there is a need for results. Returns timeout information if no results are run more than once. Let the client connect again.

$times
								=
								0;
function Read ($id)
{
	$link
								= getmysq ();
	$rs
								=
								mysql_query ("Select Id,msg,t from C where id>". $id);
	Returns the result $result the JSON format
								=
								"[";
	$ns
								=
								true;
	while ($row
								=
								mysql_fetch_object ($rs))
	{
		$result
								=
								$result. " {"M": "". $row->msg. "", "." " I ":" "$row->id." "},";
		$ns
								=
								false;
	}
	$result
								=
								$result. " {}]";
	if ($ns)
	{
		mysql_close ($link);
		$times + +;
		5 requests also did not respond, do timeout judgment, let the client request
		if ($times
								>=
								5)
		{	
			echo
								"[]";
			return;
		}
		If there is currently no readable data, wait 1 seconds to read again, if it takes a shorter time (this is quicker), use Usleep, the unit is millisecond sleep
		(1);
		Read ($id);
	}
	else
	{
		echo
								$result;
		Mysql_close ($link);
	}

As code:

//http://localhost/ajax/chat.php?type=r&id=1 var lv:loadvars = new Load
Vars ();
Lv.load ("http://localhost/ajax/chat.php?type=r&id=2");
          Lv.ondata = function (d) {trace (d) var maxid = 0;
	JSON please go to json.org to download var data = (new JSON ()). Parse (d);
	str = "";
		for (var i=0;i<data.length-1;i++) {str + = data[i].m+ ' \ n ';
	Maxid = data[i].i;
	} M.text + = str;
	M.scroll = M.maxscroll;
Lv.load ("http://localhost/ajax/chat.php?type=r&id=" +MAXID);
}///Key.addlistener (this);
	OnKeyDown = function () {if (Key.getascii () = () {if (selection.getfocus () = = "_level0.smsg") sendmsg ();
								}     send.onpress = function () {sendmsg ();}   function sendmsg () {if (Smsg.text.length >
		1) {var slv:loadvars = new Loadvars ();
		Slv.load ("http://localhost/ajax/chat.php?msg=" +smsg.text);
	Smsg.text = ""; }
}

Flash source file Download:http://roading.net/res/blog/pic/httpchat.fla

Complete PHP Code:

___fckpd___2
get["id"]; Read ($id); } else {$msg =

The first two years have been a socket based flash chat code, but not all cases will choose (or have the conditions) to use the socket of the server. Real-time communication based on HTTP can be used in programs that are not too high on the demand for immediacy.

The principle is to obtain information through the client request, if there is no need for content, the background program will wait for a period of time (reaction speed), and then see if there is a need for results. Returns timeout information if no results are run more than once. Let the client connect again.

$times
								=
								0;
function Read ($id)
{
	$link
								= getmysq ();
	$rs
								=
								mysql_query ("Select Id,msg,t from C where id>". $id);
	Returns the result $result the JSON format
								=
								"[";
	$ns
								=
								true;
	while ($row
								=
								mysql_fetch_object ($rs))
	{
		$result
								=
								$result. " {"M": "". $row->msg. "", "." " I ":" "$row->id." "},";
		$ns
								=
								false;
	}
	$result
								=
								$result. " {}]";
	if ($ns)
	{
		mysql_close ($link);
		$times + +;
		5 requests also did not respond, do timeout judgment, let the client request
		if ($times
								>=
								5)
		{	
			echo
								"[]";
			return;
		}
		If there is currently no readable data, wait 1 seconds to read again, if it takes a shorter time (this is quicker), use Usleep, the unit is millisecond sleep
		(1);
		Read ($id);
	}
	else
	{
		echo
								$result;
		Mysql_close ($link);
	}

As code:

//http://localhost/ajax/chat.php?type=r&id=1 var lv:loadvars = new Load
Vars ();
Lv.load ("http://localhost/ajax/chat.php?type=r&id=2");
          Lv.ondata = function (d) {trace (d) var maxid = 0;
	JSON please go to json.org to download var data = (new JSON ()). Parse (d);
	str = "";
		for (var i=0;i<data.length-1;i++) {str + = data[i].m+ ' \ n ';
	Maxid = data[i].i;
	} M.text + = str;
	M.scroll = M.maxscroll;
Lv.load ("http://localhost/ajax/chat.php?type=r&id=" +MAXID);
}///Key.addlistener (this);
	OnKeyDown = function () {if (Key.getascii () = () {if (selection.getfocus () = = "_level0.smsg") sendmsg ();
								}     send.onpress = function () {sendmsg ();}   function sendmsg () {if (Smsg.text.length >
		1) {var slv:loadvars = new Loadvars ();
		Slv.load ("http://localhost/ajax/chat.php?msg=" +smsg.text);
	Smsg.text = ""; }
}

Flash source file Download:http://roading.net/res/blog/pic/httpchat.fla

Complete PHP Code:

___fckpd___2
get["MSG"]; Write ($msg); function Write ($msg) {$link = GETMYSQ ();//If more than 20 data, clear $rs = mysql_query ("SELECT count (ID) as Num from C" "); $row = Mysql_fetch_object ($RS); if ($row->num >) {$rs = mysql_query (' delete from C ');}//$rs = mysql_query ("INSERT INTO C (msg,t) VALUES ("$msg", 1) "); $id = mysql_insert_id (); Mysql_close ($link); }//$times = 0; function Read ($id) {$link = GETMYSQ (); $rs = mysql_query ("Select Id,msg,t from C where id>". $id);//$resu lt = "["; $ns = true; while ($row = Mysql_fetch_object ($rs)) {$result = $result. " {"M": "". $row->msg. "", "." " I ":" "$row->id." "},"; $ns = false; } $result = $result. " {}]"; if ($ns) {mysql_close ($link); $times + +;//5 request also does not respond, make timeout judgment, let the client request if ($times >= 5) {echo "[]"; return;}// If there is currently no readable data, wait 1 seconds to reread the sleep (1); Read ($id); else {echo $result; Mysql_close ($link);}} function getmysq () { $DBPW = "123456"; $dbname = "Chat"; $link = mysql_connect ("localhost", "root", $DBPW) or die ("Could not connect:". Mysql_error ()); mysql_select_db ($dbname); return $link; }?>



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.