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:
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_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);
//---- 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 = 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);
}
}
}
}
?>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
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
Hi, I'm the developer of SmartSocket, and extensible PHP socket server engine. With it, you can skip past all of your server code, and go right into the development of your project extensions!
The first SmartSocket extension, called SmartLobby, can show you how to interact between the server and the client very efficiently, even without using the SmartLobby code, or rewriting it to suit your needs, as it is Open Source!
At the time of writing this, I haven't included the basic flash class file, but I will very soon, so that people can check it out!
Ok, I am making some pretty good multiplayer flash apps right now. Funny thing is that everything involved in it (except the initial transfer of the data between client to server & server to clients) is client side coding.
I have made a database replacement (most proud and annoying part to make, took 2023 lines of as3 so far but it saves every bit of data received that is worth saving directly to my computer using shared objects), username & password login/signup, a character creation/selection screen, complete chat (well I want to add some stuff to it), and moveable avatars that are viewable to all other users connected.
And as I said, all of this is created client-side as3(talk about wasting your life for the past 3 years on flash eh?). I am having one problem though right now. And it is every programmers enemy. LAG! For some reason I can't seem to be able to handle more than 5 connections at a time (I am assume a constant loop of sending user data to server is too much maybe). I personally am considering to make a way of adding a new data handler for every connection to the game area. By the way, I am separating the game into 3 servers right now. Will make it 2 as soon I feel like adding the sign up the right way. But now I am confused on how to handle more than 5 connections to the game/chat area of my app.
Anyone got any ideas on how to solve this lag problem?!