Example #1
0
 public function dispatchReceivePacket(Packet $packet, Session $session)
 {
     if ($session->isAuth()) {
         return true;
     }
     /* @var \jegarn\session\SwooleSession $session
      * @var AuthPacket $pkt
      */
     if ($pkt = AuthPacket::getPacketFromPacket($packet)) {
         if ($pkt->auth()) {
             $session->setUserId($pkt->getUid());
             $session->setAuth(true);
             $session->setSessionId(TextUtil::generateGUID());
             SessionManager::getInstance()->addSession($session);
             $pkt->setStatus(AuthPacket::STATUS_AUTH_SUCCESS);
         } else {
             $pkt->setStatus(AuthPacket::STATUS_AUTH_FAILED);
             Logger::addInfo('auth failed(fd:' . $session->getFd() . ')');
         }
     } else {
         $pkt = new AuthPacket();
         $pkt->setPacket($packet);
         $pkt->setStatus(AuthPacket::STATUS_NEED_AUTH);
         Logger::addInfo('auth needed(fd:' . $session->getFd() . ')');
     }
     ServerManager::getInstance()->send($session, $pkt->getReversedPacket());
     return false;
 }
 public function dispatchReceivePacket(Packet $packet, Session $session)
 {
     if (!$session->isAuth()) {
         return;
     }
     /* @var \jegarn\session\SwooleSession $session
      * @var GroupChatPacket $pkt
      */
     if ($pkt = ChatroomPacket::getPacketFromPacket($packet)) {
         // check send user is a member of this group
         $cm = ChatroomManager::getInstance();
         if ($packet->isFromSystemUser() || $cm->isGroupUser($pkt->getGroupId(), $pkt->getFrom())) {
             $fromUid = $pkt->getFrom();
             if ($uidList = $cm->getGroupUsers($pkt->getGroupId())) {
                 $sm = ServerManager::getInstance();
                 foreach ($uidList as $uid) {
                     if ($uid != $fromUid) {
                         $pktCopy = clone $pkt;
                         $pktCopy->setTo($uid);
                         if ($toSession = SessionManager::getInstance()->getSessionByUserId($uid)) {
                             Logger::addInfo('user online,chatroom(uid:' . $uid . ')');
                             $sm->send($toSession, $pktCopy);
                         } else {
                             Logger::addInfo('user not online,chatroom,abandon message(uid:' . $uid . ')');
                         }
                     }
                 }
             } else {
                 Logger::addInfo('chatroom ' . $pkt->getGroupId() . ' users lost, from uid ' . $pkt->getFrom());
             }
         } else {
             Logger::addInfo('chatroom ' . $pkt->getGroupId() . ' has no user ' . $pkt->getFrom() . ' and it is not a system user');
         }
     }
 }
 public function dispatchOfflinePacket(Session $session)
 {
     // if(!$session->isAuth() || !$session->getUserId()) return; // before dispatch, both two is checked
     $omm = OfflineNotificationMessageManager::getInstance();
     $uid = $session->getUserId();
     $sm = ServerManager::getInstance();
     /* @var ChatPacket $packet */
     while (($packet = $omm->getPacket($uid)) && $packet instanceof NotificationPacket) {
         $sm->send($session, $packet);
     }
 }
 protected function sendOrSavePacket(GroupChatPacket $packet, $uid)
 {
     $pkt = clone $packet;
     $pkt->setTo($uid);
     if ($toSession = SessionManager::getInstance()->getSessionByUserId($uid)) {
         Logger::addInfo('user online,groupchat(uid:' . $uid . ')');
         ServerManager::getInstance()->send($toSession, $pkt);
     } else {
         Logger::addInfo('user not online,groupchat,storage message(uid:' . $uid . ')');
         OfflineGroupMessageManager::getInstance()->addPacket($uid, $pkt);
     }
 }
Example #5
0
<?php

use jegarn\cache\Cache;
use jegarn\log\FileLoggerHandler;
use jegarn\log\Logger;
use jegarn\manager\ServerManager;
use jegarn\manager\PacketManager;
use jegarn\server\SwooleServer;
require __DIR__ . '/../../src/bootstrap.php';
$config = (require __DIR__ . '/config/server.php');
isset($argv[1]) && ($config['server']['host'] = $argv[1]);
isset($argv[2]) && ($config['server']['port'] = $argv[2]);
Cache::getInstance()->initConfig($config['cache']);
Logger::addHandler(new FileLoggerHandler($config['file_logger']));
foreach ($config['listener'] as $listener) {
    PacketManager::getInstance()->addListener(new $listener());
}
ServerManager::getInstance()->addServer(new SwooleServer())->initConfig($config['server'])->start();