Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extra informations #24

Closed
rashcms opened this issue Jul 5, 2016 · 2 comments
Closed

extra informations #24

rashcms opened this issue Jul 5, 2016 · 2 comments
Labels

Comments

@rashcms
Copy link

rashcms commented Jul 5, 2016

Is it possible to receive extra information from the client ?
For example :

$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 . ' and extra information is : ' . $socket->extra,
            'numUsers' => $numUsers
        ));
    });

How to send and recive $socket->extra ?

@walkor
Copy link
Owner

walkor commented Jul 5, 2016

Yes you can, for example transtransmission extra information of age.

Javascript side.

socket.emit('add user', {name: 'tom', age: 13});

Server side.

$socket->on('add user', function ($user_info) use($socket){
        global $usernames, $numUsers;
        // we store the username in the socket session for this client
        $username = $user_info['name'];
        $socket->username = $username;

        // =====Here get age form client======
        $age = $user_info['age'];

        // 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,
            'age'           => $age, // ====== extra information of age =====
        ));
    });

@walkor walkor added the question label Jul 5, 2016
@rashcms
Copy link
Author

rashcms commented Jul 5, 2016

Thanks it works like a charm 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants