ไลบรารี PHP สำหรับการให้บริการ WebSockets แบบอะซิงโครนัส สร้างแอปพลิเคชันของคุณผ่านอินเทอร์เฟซที่เรียบง่าย และนำแอปพลิเคชันของคุณกลับมาใช้ใหม่โดยไม่ต้องเปลี่ยนโค้ดใดๆ เพียงแค่รวมส่วนประกอบต่างๆ เข้าด้วยกัน
ขณะนี้เรากำลังตั้งเป้าที่จะรื้อฟื้น Ratchet เพื่อให้ได้รับการอัปเดตด้วยเวอร์ชันล่าสุด และใช้เป็นจุดเริ่มต้นสำหรับการอัปเดตที่ใหญ่กว่าที่กำลังจะมีขึ้น เราต้องการความช่วยเหลือจากคุณเพื่อให้บรรลุเป้าหมายนี้ ดูตั๋ว #1054 เพื่อดูวิธีช่วยเหลือ
จำเป็นต้องมีการเข้าถึงเชลล์และแนะนำให้เข้าถึงรูท เพื่อหลีกเลี่ยงการบล็อกพร็อกซี/ไฟร์วอลล์ ขอแนะนำให้ขอ WebSockets บนพอร์ต 80 หรือ 443 (SSL) ซึ่งต้องมีการเข้าถึงรูท ในการดำเนินการนี้ ควบคู่ไปกับการซิงค์เว็บสแต็ก คุณสามารถใช้พร็อกซีย้อนกลับหรือเครื่องสองเครื่องแยกกัน คุณสามารถดูรายละเอียดเพิ่มเติมได้ในเอกสาร Conf ของเซิร์ฟเวอร์
เอกสารผู้ใช้และ API มีอยู่ในเว็บไซต์ของ Ratchet: http://socketo.me
ดูhttps://github.com/cboden/Ratchet-examples สำหรับการสาธิตการทำงานนอกกรอบโดยใช้ Ratchet
ต้องการความช่วยเหลือ? มีคำถาม? ต้องการแสดงความคิดเห็นหรือไม่? เขียนข้อความในรายชื่ออีเมลของ Google Groups
<?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!' ) ; } ;