Created
March 7, 2013 07:45
-
-
Save lisachenko/5106278 to your computer and use it in GitHub Desktop.
Stomp console chat client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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