Ratchet
Ratchet copied to clipboard
Can I use wss seveur for ngrok test ?
Hi, i'm developing a chat in PHP, it works well but i've one issue: I have this code for ther server:
`<?php
require __DIR__.'/vendor/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
define('APP_PORT', 8889);
function addMessage($conversation_id, $user_id, $content){
# connect base de donnée host nom charset user pass
$bdd = new PDO('mysql:host=localhost:8889;dbname=Chat;charset=utf8', 'root', 'root');
$bdd -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); #pour avoir message d'erreur
$request = $bdd->prepare('INSERT INTO messages (conversation_id, user_id, content, date) VALUES (?, ?, ?, NOW())'); #insere en table
$request->execute(array($conversation_id, $user_id, $content));
$request->closeCursor();
echo("message sent");
}
class ServerImpl implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId}) [server]\n";
}
public function onMessage(ConnectionInterface $conn, $msg) {
echo sprintf("New message from '%s': %s\n\n\n", $conn->resourceId, $msg);
$conn->send("received");
foreach ($this->clients as $client) { // BROADCAST
$message = json_decode($msg, true);
if ($conn !== $client) {
$client->send("new message:".$msg);
}
}
//try{
$json = json_decode($msg);
//addMessage($json->conversation, $json->user_id, $json->content);
}
public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} is gone.\n";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error occured on connection {$conn->resourceId}: {$e->getMessage()}\n\n\n";
$conn->close();
}
}
$server = IoServer::factory(
new HttpServer(
new WsServer(
new ServerImpl()
)
),
APP_PORT
);
/*$ws = new WsServer(new MyChat);
$server = IoServer::factory(new HttpServer($ws));*/
echo "Server created on port " . APP_PORT . "\n\n";
$server->run();
?>`
but i need a wss Server for safari, otherwise, safari block the connection to the ws server. How could i do that ? Thanks