walkor / phpsocket.io

A server side alternative implementation of socket.io in PHP based on workerman.
2.3k stars 510 forks source link

How to send a message to a socket from different parts of the code? #209

Open Bezlepkin opened 5 years ago

Bezlepkin commented 5 years ago

For example i have two php files. index.php, index2.php. And both should send to the socket.

` use Workerman\Worker; use PHPSocketIO\SocketIO;

// listen port 2021 for socket.io client $io = new SocketIO(2021); $io->on('connection', function($socket)use($io){ $socket->on('chat message', function($msg)use($io){ $io->emit('chat message', $msg); }); });

Worker::runAll(); ` Need to create an instance of SocketIO twice?

DavyMassoneto commented 4 years ago

Same here, how to do this?

devyaz commented 4 years ago

You import it from the main file e.g in index2.php


'index.php' ```
Then you can use it in as usual
devyaz commented 4 years ago

$io=require 'index.php'

DavyMassoneto commented 4 years ago

$io=require 'index.php'

Don't work

devyaz commented 4 years ago

What's the error you are getting?

On Fri, 21 Feb 2020 21:29 Davy Massoneto, notifications@github.com wrote:

$io=require 'index.php'

Don't work

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/walkor/phpsocket.io/issues/209?email_source=notifications&email_token=AE35ASMG7FQ7IL6GCPIHGJ3REATRLA5CNFSM4JFWPF4KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEMT2HDA#issuecomment-589800332, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE35ASPRQYNOVGYC7JG6B3DREATRLANCNFSM4JFWPF4A .

tolew1 commented 4 years ago

"Need to create an instance of SocketIO twice?"

No, The first script/file should connect to the socket then you can use the Emitter to send from other PHP files or scripts.

See here https://github.com/walkor/phpsocket.io/issues/23