Websockets with php Ratchet does not work

Asked

Viewed 220 times

0

People are studying websockets, and when trying to follow several examples found on the internet and on the site of Ratchet itself I just can’t run the server, the terminal is like this:

inserir a descrição da imagem aqui

Follow code from the example I used:

Chat.php

<?php
namespace MyApp;
require dirname(__DIR__) . '/vendor/autoload.php';
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface {
    public function onOpen(ConnectionInterface $conn) {
    }

    public function onMessage(ConnectionInterface $from, $msg) {
    }

    public function onClose(ConnectionInterface $conn) {
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
    }
}

chat-server.php

<?php
require dirname(__DIR__) . '/src/Chat.php';
use Ratchet\Server\IoServer;
use MyApp\Chat;

    require dirname(__DIR__) . '/vendor/autoload.php';

    $server = IoServer::factory(
    new Chat(),
    8080
    );

    $server->run();

I came to think it would be because of the 8080 port, but I’m doing the tests on my computer locally. What I’m doing wrong?

  • 1

    It is running. When you turn the remote php arquivo.php the terminal will be "paused" until this task is done. Run the command again and make a connection via client. If you want to free the terminal for other tasks, just run the command php -f nome-do-arquivo.php &

  • I understand, but then in case I implement an application based on a websocket server using Ratchet I need to leave the terminal open? or is there any way you can keep it running in the background constantly? @Valdeirpsr

  • 1

    To leave a lawsuit in background, just rotate nohup php -f nome-do-arquivo.php & and for the code to always run when starting/restarting the server, just create a shell script in /etc/init.d

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.