예제 #1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     if (!class_exists('Event')) {
         dl('event.so');
     }
     $listen = $input->getOption('listen');
     if ($listen === null) {
         $listen = 8080;
     }
     $this->socket = new SocketIO();
     $this->socket->on('addme', function (Event\MessageEvent $messageEvent) {
         return $this->onAddme($messageEvent);
     })->on('msg', function (Event\MessageEvent $messageEvent) {
         return $this->onMsg($messageEvent);
     });
     $this->socket->listen($listen)->onConnect(function () {
     })->onRequest('/brocast', function ($connection, \EventHttpRequest $request) {
         if ($request->getCommand() == \EventHttpRequest::CMD_POST) {
             if (($data = json_decode($request->getInputBuffer()->read(1024), true)) !== NULL) {
                 $response = $this->onBrocast($data);
             } else {
                 $response = new Response('fail', Response::HTTP_NOT_FOUND);
             }
         } else {
             $response = new Response('fail', Response::HTTP_NOT_FOUND);
         }
         $connection->sendResponse($response);
     })->dispatch();
 }
예제 #2
0
 /**
  *
  *
  * @param [array] $config parameter for server
  */
 public function __construct($config)
 {
     $config = new Config($config);
     $io = new SocketIO($config->get('port'));
     $io->on('connection', array($this, 'onConnect'));
     $users = new Collection('\\JustText\\Models\\User');
     $rooms = new Collection('\\JustText\\Models\\Room');
     $this->service = new Container(new Factory());
     $this->service->set('io', $io);
     $this->service->set('users', $users);
     $this->service->set('rooms', $rooms);
     $this->service->set('config', $config);
 }
예제 #3
0
$senderIO->on('connection', function ($socket) {
    // Login event
    $socket->on('start_login', function ($uid) use($socket) {
        global $uidConnectionMap, $lastOnlineCount, $lastOnlinePageCount;
        if (isset($socket->uid)) {
            return;
        }
        // Refresh user id
        $uid = (string) $uid;
        if (!isset($uidConnectionMap[$uid])) {
            $uidConnectionMap[$uid] = 0;
        }
        // Save count of links to each user
        ++$uidConnectionMap[$uid];
        // For uid push
        $socket->join($uid);
        $socket->uid = $uid;
        // Send updated data
        $socket->emit("update_online_user_counts", json_encode(array("data" => $lastOnlineCount)));
    });
    // On disconnect
    $socket->on('disconnect', function () use($socket) {
        if (!isset($socket->uid)) {
            return;
        }
        global $uidConnectionMap, $senderIO;
        // No users online
        if (--$uidConnectionMap[$socket->uid] <= 0) {
            unset($uidConnectionMap[$socket->uid]);
        }
    });
});
예제 #4
0
$io->on('connection', function ($socket) {
    $socket->addedUser = false;
    // when the client emits 'new message', this listens and executes
    $socket->on('new message', function ($data) use($socket) {
        // we tell the client to execute 'new message'
        $socket->broadcast->emit('new message', array('username' => $socket->username, 'message' => $data));
    });
    // when the client emits 'add user', this listens and executes
    $socket->on('add user', function ($username) use($socket) {
        global $usernames, $numUsers;
        // we store the username in the socket session for this client
        $socket->username = $username;
        // add the client's username to the global list
        $usernames[$username] = $username;
        ++$numUsers;
        $socket->addedUser = true;
        $socket->emit('login', array('numUsers' => $numUsers));
        // echo globally (all clients) that a person has connected
        $socket->broadcast->emit('user joined', array('username' => $socket->username, 'numUsers' => $numUsers));
    });
    // when the client emits 'typing', we broadcast it to others
    $socket->on('typing', function () use($socket) {
        $socket->broadcast->emit('typing', array('username' => $socket->username));
    });
    // when the client emits 'stop typing', we broadcast it to others
    $socket->on('stop typing', function () use($socket) {
        $socket->broadcast->emit('stop typing', array('username' => $socket->username));
    });
    // when the user disconnects.. perform this
    $socket->on('disconnect', function () use($socket) {
        global $usernames, $numUsers;
        // remove the username from global usernames list
        if ($socket->addedUser) {
            unset($usernames[$socket->username]);
            --$numUsers;
            // echo globally that this client has left
            $socket->broadcast->emit('user left', array('username' => $socket->username, 'numUsers' => $numUsers));
        }
    });
});
예제 #5
0
$sender_io->on('connection', function ($socket) {
    // 当客户端发来登录事件时触发
    $socket->on('login', function ($uid) use($socket) {
        global $uidConnectionMap, $last_online_count, $last_online_page_count;
        // 已经登录过了
        if (isset($socket->uid)) {
            return;
        }
        // 更新对应uid的在线数据
        $uid = (string) $uid;
        if (!isset($uidConnectionMap[$uid])) {
            $uidConnectionMap[$uid] = 0;
        }
        // 这个uid有++$uidConnectionMap[$uid]个socket连接
        ++$uidConnectionMap[$uid];
        // 将这个连接加入到uid分组,方便针对uid推送数据
        $socket->join($uid);
        $socket->uid = $uid;
        // 更新这个socket对应页面的在线数据
        $socket->emit('update_online_count', "当前<b>{$last_online_count}</b>人在线,共打开<b>{$last_online_page_count}</b>个页面");
    });
    // 当客户端断开连接是触发(一般是关闭网页或者跳转刷新导致)
    $socket->on('disconnect', function () use($socket) {
        if (!isset($socket->uid)) {
            return;
        }
        global $uidConnectionMap, $sender_io;
        // 将uid的在线socket数减一
        if (--$uidConnectionMap[$socket->uid] <= 0) {
            unset($uidConnectionMap[$socket->uid]);
        }
    });
});