Hi.
Ich brauch eure hilfe bei meinem problem. und zwar:
Ich will eine php-socketverbindung einrichten und hab dazu ein paar codeschnipsel gefunden:
jedoch meldet mir die Flash Client-Datei leider immer wieder nur "Serverconnection failed".
Ich weiß nicht, woran es liegen kann.
- Ich hab die adressen dort je mit meiner Seite "shadows-light-world.de" ersetzt.
- Der Flash-Film darf laut veröffentlichungseinstellungen auf das netzwerk zugreifen,
- der film und die php-datei sind in das verzeichnis:
http://www.shadows-light-world.de/socketverb (intern (bei filezilla erkannt) /html/socketverb) hochgeladen.
- Da ich den server nicht selbst besitze und keine andere möglichkeit wüsste, wie ich diese php-datei intern vom server sonst ausführen lasse (ich hab von meinem webhoster her eine confixx-oberfläche), hab ich folgendes Cronetab erstellt, dass eigentlich die php alle 5min ausführen müsste:
*/5 * * * * php ./html/socketverb/socketTut.php
- ich hab die Dateiattribute von der oben genannten /html/socketverb/socketTut.php -Datei auf -rwxr-xr-x geändert.
Aber wie gesagt, meldet die flash-datei (mit installiertem FlashPlayer 10), wenn ich sie in
http://www.shadows-light-world.de/so...socketsTut.swf ausführe, ihr "Serverconnection failed".
Muss ich noch etwas zusätliches machen?? oder liegt der fehler irgedwo im code??
Ich bin mit meinem Latein am Ende =( =( =( =( =(
Wenn ich versuche die php-datei im browser zu öffnen, bekomm ich einen 500-error zurück =( =(
Bitte bitte helft mir bei diesem Problem
shadow_zed
Hier ist der Code
Für den Flash AS2-Code:
PHP-Code:
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("shadows-light-world.de", 9999);
//--- Handle button click --------------------------------------
function msgGO() {
if (inputMsg.htmlText != "") {
mySocket.send(inputMsg.htmlText+"\n");
inputMsg.htmlText = "";
}
}
pushMsg.onRelease = function() {
msgGO();
};
Für die PHP-Socketverbindung:
PHP-Code:
#!/usr/bin/php -q
<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$address = 0.0.0.0;
$port = 9999;
//---- Function to Send out Messages to Everyone Connected ----------------------------------------
// $allclient is an array of resource id's that php will refer to for the sockets
// $socket contains the resource id for the flash client who sent the message
// $buf is just the message they sent, we are sending to everyone using a foreach loop
function send_Message($allclient, $socket, $buf) {
foreach($allclient as $client) {
// socket_write uses the $client resource id (which calls on the socket) and sends it our message (which is the second flag)
socket_write($client, "$socket wrote: $buf");
}
}
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);
// now we need to bind our socket to the address and port specified.
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";
}
// this will allow an easy way to keep track of sockets connected to our server
$read_sockets = array($master);
//---- Create Persistent Loop to continuously handle incoming socket messages ---------------------
while (true) {
$changed_sockets = $read_sockets;
// socket_select is used here to see if there were any changes with the sockets that were connected before
// $num_changed_sockets is here so you can check to see if the change is true or not with a subsequent function
$num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL);
// we run a foreach function on $changed_sockets to see whether it needs to be added (if new), determine the message it sent
// or determine if there was a socket disconnect
foreach($changed_sockets as $socket) {
// now if the $socket currently being checked is the $master socket, we need to run some checks
// if not then we will skip down to else and check the messages that were sent
if ($socket == $master) {
// socket_accept will accept any incoming connections on $master, and if true, it will return a resource id
// which we have set to $client. If this did not work then return an error, but if it worked, then add in
// $client to our $read_sockets array at the end.
if (($client = socket_accept($master)) < 0) {
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
continue;
} else {
array_push($read_sockets, $client);
}
} else {
// socket_recv just receives data from $socket as $buffer, with an integer length of 2048, and a mystery flag set to 0
// that mystery flag has no real documentation so setting it to 0 will solve it as others have done.
// we've also set it as a var $bytes in case we need to ensure data sending with socket_write, which is optional
$bytes = socket_recv($socket, $buffer, 2048, 0);
// if the $bytes we have is 0, then it is a disconnect message from the socket
// we will just search for it as an index and unset that socket from our $read_sockets and
// finish up with using socket_close to ensure it is closed off
if ($bytes == 0) {
$index = array_search($socket, $read_sockets);
unset($read_sockets[$index]);
socket_close($socket);
}else{
// we need to make sure $read_sockets isn't messed with, so setting up a new variable called $allclients
// will ensure this. We then shift the array so that our $master socket is not included to the count when
// we send our data to all the other sockets in $allclients.
$allclients = $read_sockets;
array_shift($allclients);
// just a simple call on our premade function 'send_Message' with $allclients, $socket (current one), and $buffer (the message)
send_Message($allclients, $socket, $buffer);
}
}
}
}
?>