walkor / phpsocket.io-emitter

23 stars 9 forks source link

Could you add some server-side examples #1

Closed yuchenglin closed 8 years ago

yuchenglin commented 8 years ago

I came from this issue. I also want to send message from separate php app. When I connect to localhost:2022, I cannot get any log msg on server. Which part did I miss? It might be added into document. Thanks Server side:

// =======Channel server========
$channel = new Channel\Server();
$io = new SocketIO(2020);
// ========ChannelAdapter========
$io->on('workerStart', function()use($io){
    $io->adapter('\PHPSocketIO\ChannelAdapter');
});
$io->on('connection', function($socket){

    $socket->on('new message', function ($data)use($socket){
        echo $data;
        // $socket->broadcast->emit...

    });
});

$web = new WebServer('http://0.0.0.0:2022');
$web->addRoot('localhost', __DIR__ . '/public');
Worker::runAll();

Client side /public/index.php:

$emitter = new Emitter();
$emitter->emit('new message', 'TEST MESSAGE!!');
walkor commented 8 years ago

If your separate php app is not on your Channel\Server, You should set ip to Emitter like this. Client side /public/index.php.

$emitter = new Emitter('your_channel_server_ip');
$emitter->emit('new message', 'TEST MESSAGE!!');
yuchenglin commented 8 years ago

I only want to trigger the server event on different php page, and I test on local now. So it seems not that problem.

worker         listen                   processes status
PHPSocketIO    socketIO://0.0.0.0:2020   1         [OK] 
ChannelServer  frame://0.0.0.0:2206      1         [OK] 
WebServer      http://0.0.0.0:2022       1         [OK] 
walkor commented 8 years ago

When I connect to localhost:2022

Your client should connect to PHPSocketIO port 2020 for your case, not 2022.

yuchenglin commented 8 years ago

When I connect to localhost:2022

I mean that I used the browser to enter localhost:2022 in order to open index.php(separated php script). Do you mean I need to add codes to connect to PHPSocketIO before my client app (index.php)?

walkor commented 8 years ago

Do you mean I need to add codes to connect to PHPSocketIO before my client app (index.php)?

I mean you should connect to port 2020 by socket.io-client in your browser.

yuchenglin commented 8 years ago

Thanks for your help! It works on the chatting room example(php socketio). If I learn some helpful explanations, I would like to add to the document.

walkor commented 8 years ago

You are welcome and thank you so much.