Tout nouveau dans le monde du websocket, je tente de lancer le serveur websocket avec la librairie
Rachet.
J'ai installé Composer, mis en place Rachet et ses dépendances.
Je tente maintenant de lancer bin/chat-server.php.
Quand je le lance j'ai l'erreur
Uncaught InvalidArgumentException: Given URI "tcp://example.com:8080" does not contain a valid host IP (EINVAL). (example.com est bien sûr remplacé par mon domaine)
Vu le message, je me doute bien qu'il faille une IP à la place d'un nom de domaine, le problème étant que je suis un serveur mutualisé, j'ai mis l'adresse IP du serveur mutualisé mais il ne veut pas non plus. je suis chez OVH.
Voilà mon bin/chat-server.php :
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
require_once dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080,
'example.com'
);
$server->run();
Et mon src/Chat.php :
<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg) {
$numRecv = count($this->clients) - 1;
echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
, $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
foreach ($this->clients as $client) {
if ($from !== $client) {
// The sender is not the receiver, send to each client connected
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn) {
// The connection is closed, remove it, as we can no longer send it messages
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
Tout nouveau dans le monde du websocket, je tente de lancer le serveur websocket avec la librairie [url=http://socketo.me/docs/hello-world]Rachet[/url].
J'ai installé Composer, mis en place Rachet et ses dépendances.
Je tente maintenant de lancer bin/chat-server.php.
Quand je le lance j'ai l'erreur [b]Uncaught InvalidArgumentException: Given URI "tcp://example.com:8080" does not contain a valid host IP (EINVAL)[/b]. (example.com est bien sûr remplacé par mon domaine)
Vu le message, je me doute bien qu'il faille une IP à la place d'un nom de domaine, le problème étant que je suis un serveur mutualisé, j'ai mis l'adresse IP du serveur mutualisé mais il ne veut pas non plus. je suis chez OVH.
Voilà mon bin/chat-server.php :
[PHP]use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
require_once dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080,
'example.com'
);
$server->run();[/PHP]
Et mon src/Chat.php :
[PHP]<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg) {
$numRecv = count($this->clients) - 1;
echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
, $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
foreach ($this->clients as $client) {
if ($from !== $client) {
// The sender is not the receiver, send to each client connected
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn) {
// The connection is closed, remove it, as we can no longer send it messages
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}[/PHP]