Git Product home page Git Product logo

easy-xmpp's Introduction

Easy-XMPPHP

XMPP library (based on xmpphp)

环境需求

PHP > 5.x

安装

$ composer require "mark-fish/easy-xmpp"

使用

use EasyXmpp\XMPP;
use EasyXmpp\Util\Log;

$config = [
        "host"=>"",         // host [required]
        "port"=>"",         // tcp port in server config file [required]
        "user"=>"",         // register user account [required]
        "password"=>"",     // password [required]
        "resource"=>"",     // resource [optional]
        "server"=>"",       // ip [optional]
        "printlog"=>true,   // debug [optional]
        "loglevel"=>Log::DEBUG, // debug level [optional]
        "timeout"=>30,      // connect timeout (s) [optional]
        "persistent"=>true,  // connect persist [optional]
        "logfile"=>"/tmp/xmpp.log" // [optional]
];

$conn = new XMPP($config);
$conn->autoSubscribe();
$vcard_request = array();
try {
    $conn->connect($config['timeout'],$config['persistent']);
    while (!$conn->isDisconnected()) {
        $payloads = $conn->processUntil(array('message', 'presence', 'end_stream', 'session_start', 'vcard','reconnect'));
        foreach ($payloads as $event) {
            $pl = $event[1];
            switch ($event[0]) {
                case 'end_stream':
                    echo "receive end stream".PHP_EOL;
                    break;
                case 'reconnect':
                    echo "receive reconnect".PHP_EOL;
                    break;
                case 'message':
                    echo "---------------------------------------------------------------------------------".PHP_EOL;
                    echo "Message from: {$pl['from']}\n";
                    if (isset($pl['subject']))
                        echo "Subject: {$pl['subject']}\n";
                    echo $pl['body'] . "\n";
                    echo "---------------------------------------------------------------------------------".PHP_EOL;
                    $conn->message($pl['from'], $body = "Thanks for sending me \"{$pl['body']}\".", $type = $pl['type']);
                    $cmd = explode(' ', $pl['body']);
                    if ($cmd[0] == 'quit')
                        $conn->disconnect();
                    if ($cmd[0] == 'break')
                        $conn->send("</end>");
                    if ($cmd[0] == 'vcard') {
                        if (!($cmd[1]))
                            $cmd[1] = $conn->user . '@' . $conn->server;
                        // take a note which user requested which vcard
                        $vcard_request[$pl['from']] = $cmd[1];
                        // request the vcard
                        $conn->getVCard($cmd[1]);
                    }
                    break;
                case 'presence':
                    echo "Presence: {$pl['from']} [{$pl['show']}] {$pl['status']}\n";
                    break;
                case 'session_start':
                    echo "Session Start\n";
                    $conn->getRoster();
                    $conn->presence($status = "Cheese!");
                    break;
                case 'vcard':
                    // check to see who requested this vcard
                    $deliver = array_keys($vcard_request, $pl['from']);
                    // work through the array to generate a message
                    $msg = '';
                    foreach ($pl as $key => $item) {
                        $msg .= "$key: ";
                        if (is_array($item)) {
                            $msg .= "\n";
                            foreach ($item as $subkey => $subitem) {
                                $msg .= "  $subkey: $subitem\n";
                            }
                        } else {
                            $msg .= "$item\n";
                        }
                    }
                    // deliver the vcard msg to everyone that requested that vcard
                    foreach ($deliver as $sendjid) {
                        // remove the note on requests as we send out the message
                        unset($vcard_request[$sendjid]);
                        $conn->message($sendjid, $msg, 'chat');
                    }
                    break;
            }
        }
    }
} catch (Exception $e) {
    die($e->getMessage().PHP_EOL);
}

License

MIT

easy-xmpp's People

Contributors

lemon2015 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.