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(); };