I'm trying this bundle to make calls via node controller symfony https://github.com/bazo/tembo
but I had to add exceptions for use as Would indeed
i want to call my function 'barno' in my node
Node.js
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
server.listen(9001);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.emit('news2', { hello: 'world2' });
socket.on('message', function(data){
socket.broadcast.emit('broadcast-action', data);
});
socket.on('barno', function(data) {
console.dir(data);
socket.emit('barno', {profilo_da_seguire: '100'});
});
});
my Controller
$client = new SocketIOClient('http://localhost:9001');
$client->connect();
//emit event
$args = ['nome' => 'alessio'];
$client->emit('barno', $args);
$received = 'barno';
$pippo = array();
try {
$client->listen(function($event, Message $message = null) use (&$received, &$pippo) {
if($message !== null) {
if ($message->getName() == $received) {
$gino = $message->getArgs();
if (!is_object($gino[0])) {
$args = json_decode(current($message->getArgs()));
}else{
$args = $message->getArgs();
$pippo[] = $args[0];
}
throw new \Exception('Load');
}
/*$message = sprintf('packet: %d, time: %f, heartbeats: %d', $args->packet, $args->time, $args->heartbeats);
writeDebug($message);*/
}
});
} catch (\RuntimeException $e) {
echo $e->getMessage();
} catch (\Exception $e) {
}
//var_dump($pippo);
foreach ($pippo as $i => $pluto) {
$pippo[$i]->nome= 'barno';
}
$client->disconnect(1);
return array(
'pippo' => $pippo
);
}
whit the Xdebug I see That When the code arrive here
if ($message->getName() == $received)
I have these calls
$message->getName() = "news"
$message->getName() = "news2"
$message->getName() = "barno"
i want only call the function Barno, how can i do this?
Than i add this
throw new \Exception('Load');
because the code entered in the loop
the result is
array (size=1)
0 =>
object(stdClass)[53]
public 'profilo_da_seguire' => string '100' (length=3)
public 'nome' => string 'barno' (length=5)
And this is correct but after unless calls
Remove:
socket.emit('news', { hello: 'world' });
socket.emit('news2', { hello: 'world2' });
lines from your node.js code.
Whenever the php client connects to node those lines are fired.
These two emits:
socket.emit('news', { hello: 'world' });
socket.emit('news2', { hello: 'world2' });
happen upon 'connect'. That means they will be emitted every time someone connects. And you DO connect. If you only want to use them for special purposes, create new listeners for them.