Jump to content


Photo

Chat with php socket server


  • Please log in to reply
2 replies to this topic

#1 blasters

blasters

    Young Padawan

  • Members
  • Pip
  • 5 posts

Posted 01 April 2010 - 05:46 AM

Hi,

I'm working on a flash based chat. And now I come to the step I have no more idea. How can I create an online list and how can I make so that your nickname is added to the message.

This will be a multi-room chat.

The code is added below is taken from something I found on the net. With some modifications.

Hoping for quick answer:)


Server.php - Socket server script
<?php
//Settings 
$address = "127.0.0.1";
$port = "1234"; 
//Dont time out
set_time_limit(0);
error_reporting(E_ALL);
ob_implicit_flush();
//Send message function
function send_Message($allclient, $socket, $buf) {
foreach($allclient as $client) {
socket_write($client, "$socket wrote: $buf");
}
}
//Start socket creation
if (($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
echo "socket_create() failed, reason: " . socket_strerror($master) . "\n";
}
socket_set_option($master, SOL_SOCKET,SO_REUSEADDR, 1);
if (($ret = socket_bind($master, $address, $port)) < 0) {
echo "socket_bind() failed, reason: " . socket_strerror($ret) . "\n";
}
if (($ret = socket_listen($master, 5)) < 0) {
echo "socket_listen() failed, reason: " . socket_strerror($ret) . "\n";
}
$read_sockets = array($master);
while (true) {
$changed_sockets = $read_sockets;
$num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL);
foreach($changed_sockets as $socket) {
if ($socket == $master) {
if (($client = socket_accept($master)) < 0) {
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
continue;
} else {
array_push($read_sockets, $client);
}
} else {
$bytes = socket_recv($socket, $buffer, 2048, 0);
if ($bytes == 0) {
$index = array_search($socket, $read_sockets);
unset($read_sockets[$index]);
socket_close($socket);
}else{
$allclients = $read_sockets;
array_shift($allclients);
send_Message($allclients, $socket, $buffer);
//Close all 
   }
}

}
}
?>

Action script 2.0 - Chat client
mySocket = new XMLSocket();
mySocket.onConnect = function(success) {
   if (success) {
	  msgArea.htmlText += "<b>Server connection established!</b>";
   } else {
	  msgArea.htmlText += "<b>Server connection failed!</b>";
   }
};
mySocket.onClose = function() {
   msgArea.htmlText += "<b>Server connection lost</b>";
};
XMLSocket.prototype.onData = function(msg) {
   msgArea.htmlText += msg;
};
mySocket.connect("127.0.0.1", 1234);
function msgGO() {
   if (inputMsg.htmlText != "") {
	  mySocket.send(inputMsg.htmlText+"\n");
	  inputMsg.htmlText = "";
   }
}
pushMsg.onRelease = function() {
   msgGO();
};


#2 camboreadcenter

camboreadcenter

    Young Padawan

  • Members
  • Pip
  • 31 posts
  • Gender:Male
  • Location:Phnom Penh

Posted 05 May 2010 - 11:20 AM

me tooo also have this problem but not yet have soluction

#3 mithailebaiker

mithailebaiker

    Young Padawan

  • Members
  • Pip
  • 5 posts

Posted 09 July 2010 - 01:28 AM

Most PHP applications function have found it necessary meta refreshes, hidden frames, a large number of Javascript. They were clumsy, buggy and maintenance required too much bandwidth consumed, and only were't do the job.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users