jaxl / JAXL

Asynchronous, non-blocking I/O, event based PHP library for writing custom TCP/IP client and server implementations. From it's previous versions, library inherits a full blown stable support for XMPP protocol stack. In v3.0, support for HTTP protocol stack was also added.
http://jaxl.readthedocs.org/
Other
379 stars 121 forks source link

client socket not closed #38

Open trueicecold opened 11 years ago

trueicecold commented 11 years ago

We're having heavy usage of JAXL in our Facebook App to send chat messages to users by using a slightly modified xfacebook_platform_client.php example.

Thing is after using so many calls, we were noticing some connections are never closed (Stuck on apache as WRITING or GRACEFULLY FINISHING forever).

After a few hours - apache crashes for having to many of those.

The code we're using is:

$DOCUMENT_ROOT = getenv('DOCUMENT_ROOT');

require_once "constants_peopleroulette.php";  

$customText = (isset($_POST["customText"])) ? $_POST["customText"] : "";

$invitation_prefix = "« " . $customText . " »\n";
$invitation_suffix = "";

set_time_limit(10);

$token_dec = base64_decode(trim($_POST["chatSession"]));
if (strpos($token_dec, "_") === false)
    die();
$token_dec = explode("_", $token_dec);
if (sizeof($token_dec) != 2)
    die();

//
// initialize JAXL object with initial config
//
require_once $DOCUMENT_ROOT . '/includes/lib/jaxl/jaxl.php';

$client = new JAXL(array(
    // (required) credentials
    'jid' => trim($token_dec[0]).'@chat.facebook.com',
    'fb_app_key' => APP_ID,
    'fb_access_token' => $_POST["sessionKey"],

    // force tls (facebook require this now)
    'force_tls' => true,
    // (required) force facebook oauth
    'auth_type' => 'X-FACEBOOK-PLATFORM',

    // (optional)
    //'resource' => 'resource',

    'log_level' => JAXL_ERROR
));

//
// add necessary event callbacks here
//
$client->add_cb('on_auth_success', function() {
    global $client;
    global $token_dec;
    global $invitation_prefix;
    global $invitation_suffix;          

    _info("got on_auth_success cb, jid ".$client->full_jid->to_string());
    $client->send_chat_msg("-" . trim($token_dec[1]) . "@chat.facebook.com", $invitation_prefix . $_POST["inviteURL"] . $invitation_suffix);
    $client->send_end_stream();
});

$client->add_cb('on_auth_failure', function($reason) {
    global $client;
    $client->send_end_stream();
    _info("got on_auth_failure cb with reason $reason");
});

$client->add_cb('on_disconnect', function() {
    _info("got on_disconnect cb");
});

//
// finally start configured xmpp stream
//
$client->start();

Any idea on why this is happening? Any way of performing a timeout on the socket and forcefully close it?

ghost commented 11 years ago

I have a similar problem. I've tested the xfacebook_platform_client.php code to simply connect to the facebook chat and drop a message. The PHP code is hosted on a test domain and i've also created a test fb-app. I've used my Facebook account for the tests.

Unfortunally, now everyone who sends me a chat message on FB, gets replied with four identical messages, because 4 is the number of times i've reloaded the script.

I think i've created 4 chat bots and i don't know how to kill them! I've tried cleaning the browser caches, deleting the php script and the facebook app, but JAXL is still running. Do you know anything helpful?

frost-nzcr4 commented 10 years ago
$client = new JAXL(...)
posix_kill($client->pid, SIGINT);