Skip to content

Instantly share code, notes, and snippets.

@lisachenko
Created March 7, 2013 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lisachenko/5106278 to your computer and use it in GitHub Desktop.
Save lisachenko/5106278 to your computer and use it in GitHub Desktop.
Stomp console chat client
<?php
$author = trim(`whoami`);
$pid = pcntl_fork();
if ($pid === 0) {
// Child process here
$stomp = new Stomp();
$stomp->subscribe('/topic/Chat');
while (true) {
$frame = $stomp->readFrame();
if ($frame) {
$packet = json_decode($frame->body, true);
echo date('H:m:i '), $packet['author'], ': ', $packet['text'], PHP_EOL;
$stomp->ack($frame);
}
}
} elseif ($pid !== -1) {
echo "Simple chat client, please type 'q' to exit or type anything and press enter", PHP_EOL;
$stomp = new Stomp();
while (true) {
$line = trim(fgets(STDIN));
if ($line=='q') {
break;
}
$message = json_encode(array(
'text' => $line,
'author' => $author,
));
$stomp->send('/topic/Chat', $message);
}
posix_kill($pid, 9);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment