Code2Design.com

User login

The Layout

Programming

Graphic Design

Resources

Navigation

C2D Projects

Unsystematic Affiliates

Tutorial-Search Proof That God Exists Tutorials Expert ITexperts Central 

Change Language

Who's online

There are currently 0 users and 9 guests online.

PHP + XML Socket Server

Hey everyone,
I'm looking for either a tutorial or a completely done server. Here's what I have so far: I have over 100 different "worlds" on my website that each need to have a subdirectory of chat rooms.
IE:

Quote:
PHP DEVELOPMENT (category)
--->Socket Servers(room)
--->MySQL Integration(room)
--->Random Talk(room)

AJAX DEVELOPMENT(category)
--->Nifty XML(room)
--->Not detergent(room)

TK DEVELOPMENT(category)
--->What you can do to help(room)
--->TK is an endangered species(room)
--->Save the TK Fund(room)
--->That guy is weird(room)
--->Open Discussion or something like that(room)

The categories are predefined by a database I've got. Users need to be able to create a chat room in a category, and I need to be able to list all rooms out in a category so users can chat in rooms relevant to their subject. The engine needs to interpret the messages and send the messages to the right chat rooms and categories. I'm assuming that I'll be needing to print out XML so that flash can interpret it or the like. Each user will be carrying a variable that determines what category they are browsing so when they come to my chat 'page' they'll see all the open chats running in their current subject as well as all the categories so they can change to a different category if need be.

I also need certain functionality. Users need to be able to whisper to one another, and there will be a host (chat-starter) for each room. The host will have the ability to boot members that are being stupid and the ability to transfer host control to another leader.

This system needs to kick major butt: stable and powerful. I'll be running it on a pretty powerful server (2 Clovertowns :) ) so my application server should be able to handle this. I'm looking at having upwards of 2000 chats running at the same time. Sounds ambitious, but I'm a worst-case-scenario kind of guy.

I've run through multiple tutorials (esp. the one on Kirupa.com) and this is the code I've come up with. It works but it has absolutely no user-login or tracking. It works fine on my localhost server, if I pull up two flash windows, it communicates just fine. Here is my php and chat codes, I don't think I need to attach the .bat file:
PHP CODE:

#!/usr/bin/php -q
<?php
//#!/usr/bin/php -q = SUPRESSES HTTP HEADERS AND DEFINES THIS AS A SOCKET
 
error_reporting(E_ALL);
 
set_time_limit(0);
 
ob_implicit_flush();
 
$address '127.0.0.1';
$port 9999;

 
//---- Function to Send out Messages to Everyone Connected ----------------------------------------
function send_Message($allclient$socket$buf) {
    foreach(
$allclient as $client) {
       
$buf.substr_replace("tk","/me",0);
       
socket_write($client"$socket wrote: ".$buf);
    }
}
 
 
 
//---- Start Socket creation for PHP 5 Socket Server -------------------------------------
if (($master socket_create(AF_INETSOCK_STREAMSOL_TCP)) < 0) {
    echo 
"socket_create() failed, reason: " socket_strerror($master) . "\n";
}

socket_set_option($masterSOL_SOCKETSO_REUSEADDR1);

if ((
$ret socket_bind($master$address$port)) < 0) {
    echo 
"socket_bind() failed, reason: " socket_strerror($ret) . "\n";
}

if ((
$ret socket_listen($master5)) < 0) {
    echo 
"socket_listen() failed, reason: " socket_strerror($ret) . "\n";
}

$read_sockets = array($master);
 
//---- Create Persistent Loop to continuously handle incoming socket messages ---------------------
while (true) {
   
$changed_sockets $read_sockets;
     
   
$num_changed_sockets socket_select($changed_sockets$write NULL$except NULLNULL);
 
    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$buffer20480);
 
            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);
            }
        }
    }
}
?>
*Note, I haven't run through this code and learned all of it yet. I'll get to that ASAP.

AS3 Code:

//REQUIRES TWO TEXTFIELDS ON THE STAGE, 'inputMsg' and 'msgArea', AND A BUTTON 'pushMsg'
package {
import flash.display.Sprite;
import flash.events.DataEvent;
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.XMLSocket;
import flash.text.TextField;
import flash.xml.XMLDocument;

public class SocketTest extends Sprite
{
private var XMLS:XMLSocket;
private var TextF:TextField;

public function SocketTest()
{
XMLS = new XMLSocket();
ConfigureListeners(XMLS);
XMLS.connect("localhost", 9999);

pushMsg.addEventListener(MouseEvent.CLICK, pushMessage);

msgArea.htmlText = "Flash is trying to connect...";
}

private function ConfigureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.CONNECT, SOCK_Connect);
dispatcher.addEventListener(Event.CLOSE, SOCK_Close);
dispatcher.addEventListener(DataEvent.DATA, SOCK_Data);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, SOCK_IOError);
dispatcher.addEventListener(ProgressEvent.PROGRESS, SOCK_Progress);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, SOCK_SError);
}

private function pushMessage(e:MouseEvent):void {
if(inputMsg.text != "") {

var xml:XMLDocument = new XMLDocument();
XMLS.send(obj.message);
inputMsg.htmlText = "";
}
}

private function SOCK_Connect(e:Event):void {
msgArea.appendText("\nFLASH HAS SUCCESSFULLY CONNECTED TO THE SERVER");
}
private function SOCK_Close(e:Event):void {
msgArea.appendText("\nTHE SERVER CONNECTION HAS BEEN LOST");
}
private function SOCK_Data(e:DataEvent):void {
msgArea.htmlText += e.data;
}
private function SOCK_IOError(e:IOErrorEvent):void {
trace("IO Error Event");
msgArea.appendText("<b>FLASH HAS ENCOUNTERED AN ERROR</b>");
}
private function SOCK_Progress(e:ProgressEvent):void {

}
private function SOCK_SError(e:SecurityErrorEvent):void {
trace("Security Error");
msgArea.appendText("<b>FLASH HAS ENCOUNTERED A SECURITY ERROR.</b>");
}

}
}

Now I don't know what the best thing to do would be... either have one centralized server that rocks the world or have a server for every category ( I don't want to rebuild a server 100+ times... please don't make me do that...). However, I'm pretty sure that xml and php would be the best way to go for me. And yes, I HAVE looked into Unity and Parsley, though I'm still a bit unclear on how to use Parsley.

So basically what I need is a solution to all of this. Has anyone developed a chat system that accommodates all of this and is easily modifyable by a Flash and PHP Developer? I've looked into so many different engines, from FlashChat to FlashPioneer. They all suck and are made for Joe Average user who wants a nifty application on his site. Does anyone know of any tutorials or solutions to what I'm talking about? I'm pretty good in PHP and I have no problem with AS3 and Flash at all.

What should I do? I'm limited on time and I need to get this sucker running ASAP. Anyone?

- TK


Submitted by rfkrocktk on February 22, 2008 - 8:36pm.
printer friendly version

PHP / FLASH Chat Server

Sorry, but I have never attempted such a thing. I don't have much experience in AS so I wouldn't know much about the two working together. Nevertheless, pixel2life is a great place to look for tutorials if you haven't been there yet. I would also look through code.google.com


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati