Advertisement
Guest User

Untitled

a guest
Nov 11th, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.56 KB | None | 0 0
  1. <?php
  2. //variable to know where the user is
  3. $chosen = 0;
  4.  
  5. echo "Enter a username on which you will receive the messages! \r\n";
  6.  
  7. //asks for a user input
  8. $user = fgets(STDIN, 1024);
  9.  
  10. //removing new line
  11. $user = rtrim($user, "\n\r");
  12.  
  13. //while to keep the script always running
  14. while ($chosen === 0) {
  15.   //shows a list of commands
  16.   echo "\r\n\r\n\r\nYou are logged in as $user \r\n
  17.  List of commands: \r\n
  18.  /send ~~ To send a message \r\n
  19.  /receive ~~ To receive a message \r\n
  20.  /exit ~~ To close the chat\r\n\r\n\r\n";
  21.  
  22.   //asks for user input
  23.   $command = fgets(STDIN, 1024);
  24.  
  25.   //removing new line
  26.   $command = rtrim($command, "\n\r");
  27.  
  28.   //setting chosen based on user input
  29.   if ($command === '/send') {
  30.       $chosen = 1;
  31.   }
  32.   if ($command === '/receive') {
  33.       $chosen = 2;
  34.   }
  35.   if ($command === '/exit') {
  36.       $chosen = 3;
  37.   }
  38.  
  39.   // Action for the /send command
  40.   while ($chosen === 1) {
  41.     echo "Type /back to go back\n\r";
  42.  
  43.     //asks for user input so I know where to send the message
  44.     echo 'Destination:';
  45.     $destination = fgets(STDIN);
  46.     $destination = rtrim($destination, "\n\r");
  47.     if ($destination === '/back') {
  48.         $chosen = 0;
  49.         break;
  50.     }
  51.  
  52.     //asks for the message content
  53.     echo "Message\r\n";
  54.     $message = fgets(STDIN);
  55.     $message = rtrim($message, "\n\r");
  56.     echo "\r\n";
  57.  
  58.     if ($message === '/back') {
  59.       $chosen = 0;
  60.       break;
  61.     } else {
  62.       //connects to the JMS server
  63.       try {
  64.           $stomp = new Stomp('tcp://localhost:61613');
  65.           while (true) {
  66.             //sends the message to the JMS server
  67.               $stomp->send($destination, $message, array('sent_by' => $user));
  68.               //break so it only sends it once
  69.               break;
  70.           }
  71.       } catch (StompException $e) {
  72.           die('Connection error: '.$e);
  73.       }
  74.     }
  75.   }
  76.  
  77.   // Action for the /receive command
  78.   while ($chosen === 2) {
  79.     //sets the queue to the chosen username
  80.     $queue = $user;
  81.  
  82.     //connects to the JMS server
  83.     $stomp = new Stomp('tcp://127.0.0.1:61613');
  84.     $stomp->subscribe($queue);
  85.     while ($stomp->hasFrame()) {
  86.       $frame = $stomp->readFrame();
  87.       if ($frame != null) {
  88.         //prints out the messages
  89.         echo "Sent by: ".$frame->headers['sent_by']."\r\n";
  90.         echo "Message: ".$frame->body."\r\n\r\n\r\n";
  91.         $stomp->ack($frame);
  92.       }
  93.     }
  94.     if (!$stomp->hasFrame()){
  95.       echo "No new messages\r\n\r\n\r\n";
  96.     }
  97.     $chosen = 0;
  98.   }
  99.  
  100.   //exit
  101.   if ($chosen === 3) {
  102.     die;
  103.   }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement