PHP Microservice Framework Swoft: WebSocket Server Bagian 1


Artikel yang akan kita pelajari adalah: Cara menginstal dan menjalankan server websocket swoft.


Artikel ini adalah salah satu dari serangkaian artikel di Server WebSoft Swoft. Mari belajar tentang Swoft!

Apa itu Swoft?


Swoft adalah kerangka kerja coroutine microservice kinerja tinggi PHP. Ini telah diterbitkan selama bertahun-tahun dan telah menjadi pilihan terbaik untuk php.


Ini bisa seperti Go, server web coroutine bawaan dan klien coroutine umum dan ada dalam memori, tidak tergantung pada PHP-FPM tradisional.


Ada operasi Go bahasa yang serupa, mirip dengan anotasi fleksibel kerangka kerja Cloud Spring.


Melalui tiga tahun akumulasi dan eksplorasi arah, Swoft telah menjadikan Swoft Spring Cloud di dunia PHP, yang merupakan pilihan terbaik untuk kerangka kerja kinerja tinggi PHP dan manajemen layanan mikro.


Github



Buat proyek baru


Gunakan alat swoft-cli untuk membuat proyek baru untuk Websocket.


 php swoftcli.phar create:app --type ws swoft-ws-app cd swoft-ws-app composer install 

Mulai server


Mulai server Websocket dengan php bin/swoft ws:start perintah, Anda dapat melihat di bawah ini :


 $ php bin/swoft ws:start Information Panel ******************************************************************************************* * WebSocket | Listen: 0.0.0.0:18308, type: TCP, mode: Process, worker: 8 ******************************************************************************************* 

Port of Websocket adalah 18308

Modul


Gunakan alat swoft-cli untuk membuat modul websocket baru.


 php swoftcli.phar gen:ws-mod echo --prefix /echo 

Kode untuk modul gema ( app/WebSocket/EchoModule.php ) seperti ini:


 <?php declare(strict_types=1); namespace App\WebSocket; use Swoft\Http\Message\Request; use Swoft\Http\Message\Response; use Swoft\WebSocket\Server\Annotation\Mapping\WsModule; use Swoft\WebSocket\Server\Annotation\Mapping\OnOpen; use Swoft\WebSocket\Server\Annotation\Mapping\OnHandshake; use Swoole\WebSocket\Server; /** * Class EchoModule - This is an module for handle websocket * * @WsModule("/echo") */ class EchoModule { /** * @OnHandshake() * @param Request $request * @param Response $response * @return array */ public function checkHandshake(Request $request, Response $response): array { // some validate logic ... return [true, $response]; } /** * @OnOpen() * @param Server $server * @param Request $request * @param int $fd * @return mixed */ public function onOpen(Server $server, Request $request, int $fd) { $server->push($fd, 'hello, welcome! :)'); } } 

Tes


Di sini menggunakan swoft-devtool untuk menghubungkan server WebSocket.


Gunakan php bin/swoft dclient:ws /echo perintah php bin/swoft dclient:ws /echo di komponen swoft-devtool untuk menghubungkan server WebSocket, Anda dapat melihat pesan koneksi sukses seperti di bawah ini.


 Begin connecting to websocket server: 127.0.0.1:18308 path: /echo Success connect to websocket server. Now, you can send message INTERACTIVE ================================================================================ server> ?Opened, welcome #1! client> hi server> Recv: hi client> 

Github


Source: https://habr.com/ru/post/id471598/


All Articles