Pustaka PHP untuk melayani WebSockets secara asinkron. Bangun aplikasi Anda melalui antarmuka sederhana dan gunakan kembali aplikasi Anda tanpa mengubah kode apa pun hanya dengan menggabungkan komponen yang berbeda.
Saat ini kami bertujuan untuk menghidupkan kembali Ratchet agar tetap up to date dengan versi terbaru dan menggunakan ini sebagai titik awal untuk pembaruan yang lebih besar di masa mendatang. Kami membutuhkan bantuan Anda untuk mencapai tujuan ini, lihat tiket #1054 untuk mengetahui cara membantu.
Akses shell diperlukan dan akses root disarankan. Untuk menghindari pemblokiran proxy/firewall, disarankan agar WebSockets diminta pada port 80 atau 443 (SSL), yang memerlukan akses root. Untuk melakukan hal ini, bersama dengan tumpukan web sinkronisasi, Anda dapat menggunakan proksi terbalik atau dua mesin terpisah. Anda dapat menemukan detail lebih lanjut di dokumen konfigurasi server.
Dokumentasi pengguna dan API tersedia di situs web Ratchet: http://socketo.me
Lihat https://github.com/cboden/Ratchet-examples untuk beberapa demo kerja out-of-the-box menggunakan Ratchet.
Butuh bantuan? Punya pertanyaan? Ingin memberikan masukan? Tulis pesan di Milis Grup Google.
<?php
use Ratchet MessageComponentInterface ;
use Ratchet ConnectionInterface ;
// Make sure composer dependencies have been installed
require __DIR__ . ' /vendor/autoload.php ' ;
/ * *
* chat . php
* Send any incoming messages to all connected clients ( except sender )
* /
class MyChat implements MessageComponentInterface {
protected $ clients ;
public function __construct () {
$ this -> clients = new SplObjectStorage ;
}
public function onOpen ( ConnectionInterface $ conn ) {
$ this -> clients -> attach ( $ conn );
}
public function onMessage ( ConnectionInterface $ from , $ msg ) {
foreach ( $ this -> clients as $ client ) {
if ( $ from != $ client ) {
$ client -> send ( $ msg );
}
}
}
public function onClose ( ConnectionInterface $ conn ) {
$ this -> clients -> detach ( $ conn );
}
public function onError ( ConnectionInterface $ conn , Exception $ e ) {
$ conn -> close ();
}
}
// Run the server application through the WebSocket protocol on port 8080
$ app = new Ratchet App ( ' localhost ' , 8080 );
$ app -> route ( ' /chat ' , new MyChat , array ( ' * ' ));
$ app -> route ( ' /echo ' , new Ratchet Server EchoServer , array ( ' * ' ));
$ app -> run ();
$ php chat.php
// Then some JavaScript in the browser:
var conn = new WebSocket ( 'ws://localhost:8080/echo' ) ;
conn . onmessage = function ( e ) { console . log ( e . data ) ; } ;
conn . onopen = function ( e ) { conn . send ( 'Hello Me!' ) ; } ;