WebSocket ตอนที่ 2 : การติดตั้ง PHP Library จาก Composer เพื่อเขียน WebSockets |
WebSocket ตอนที่ 2 : การติดตั้ง PHP Library จาก Composer เพื่อเขียน WebSockets ในการจะเขียน PHP WebSockets ในปัจจุบันมี Library มารองรับหลายตัว โดยแต่ล่ะตัวการทำงานก็ไม่ได้แตกต่างกัน ขึ้นอยู่ว่าเจ้าไหนจะทำได้ออกมาดีกว่าและสามารถทำงานได้มีประสิทธิภาพมากกว่า ซึ่งในบทความนี้ทางแอดมินเลือกใช้ Library ของ socketo.me ซึ่งเหตุลหลักๆ คือ มัน Search แล้วขึ้นมาที่ 1 นั่นเอง และแน่นอนว่าน่าจะมีคนใช้กันเยอะ และน่าจะมีการอัพเดดและปรับปรุงมาพอสมควร
Library ต่างๆ ของ PHP WebSockets ที่สามารถใช้งานได้
ในการใช้งาน Library ของ socketo.me สามารถติดตั้งได้จาก Composer ส่วน Server นั้นในบทความนี้ได้เลือกใช้ XAMPP บน PHP 5.6 ขึ้นไป
ติดตั้ง (XAMPP) เวอร์ชั่น 5.x (for Windows 7,8,10) เขียน PHP/MySQL
รู้จัก Composer คืออะไร การติดตั้งบน PHP Composer
ให้ติดตั้ง Composer และ XAMPP หรือถ้ามี Web Server อื่นๆ แล้วก็สามารถใช้งานได้เช่นเดียวกัน
เริ่มต้นให้สร้างโฟเดอร์สำหรับเก็บ Project ซึ่งในที่นี้จะสร้างชื่อว่า websocket โดยอยู่ภายใต้ Path ของ Web Server
ให้สร้างไฟล์ชื่อว่า composer.json เก็บคำสั่งดังต่อไปนี้
composer.json
{
"autoload": {
"psr-0": {
"MyApp": "src"
}
},
"require": {
"cboden/Ratchet": "0.3.*"
}
}
ไฟล์ json
ให้เปิด Command Promt เข้า Path ของ D:\xampp\htdocs\websocket> ด้วยการพิมพ์
D:\xampp\htdocs\websocket>
ให้ดู Path ของ Web Server และที่จัดเก็บไฟล์ Project ให้ถูกต้อง
จากนั้นรันคำสั่ง
composer install
ถ้าขึ้นดังรูปแสดงว่าโปรแกรมทำการดาวน์โหลดและติดตั้ง Library เรียบร้อย
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 14 installs, 0 updates, 0 removals
- Installing symfony/routing (v3.2.2) Downloading: 100%
- Installing symfony/polyfill-mbstring (v1.3.0) Downloading: 100%
- Installing symfony/http-foundation (v3.2.2) Downloading: 100%
- Installing react/promise (v2.5.0) Downloading: 100%
- Installing evenement/evenement (v2.0.0) Downloading: 100%
- Installing react/stream (v0.4.5) Downloading: 100%
- Installing react/event-loop (v0.4.2) Downloading: 100%
- Installing react/socket (v0.4.5) Downloading: 100%
- Installing symfony/event-dispatcher (v3.2.2) Downloading: 100%
- Installing guzzle/common (v3.9.2) Downloading: 100%
- Installing guzzle/stream (v3.9.2) Downloading: 100%
- Installing guzzle/parser (v3.9.2) Downloading: 100%
- Installing guzzle/http (v3.9.2) Downloading: 100%
- Installing cboden/ratchet (v0.3.6) Downloading: 100%
symfony/routing suggests installing doctrine/annotations (For using the annotation loader)
symfony/routing suggests installing symfony/config (For using the all-in-one router or any loader)
symfony/routing suggests installing symfony/dependency-injection (For loading routes from a service)
symfony/routing suggests installing symfony/expression-language (For using expression matching)
symfony/routing suggests installing symfony/yaml (For using the YAML loader)
react/event-loop suggests installing ext-libevent (>=0.1.0)
react/event-loop suggests installing ext-event (~1.0)
react/event-loop suggests installing ext-libev (*)
symfony/event-dispatcher suggests installing symfony/dependency-injection ()
symfony/event-dispatcher suggests installing symfony/http-kernel ()
Package guzzle/common is abandoned, you should avoid using it. Use guzzle/guzzle instead.
Package guzzle/stream is abandoned, you should avoid using it. Use guzzle/guzzle instead.
Package guzzle/parser is abandoned, you should avoid using it. Use guzzle/guzzle instead.
Package guzzle/http is abandoned, you should avoid using it. Use guzzle/guzzle instead.
Writing lock file
Generating autoload files
ไฟล์ Library ต่างๆ ที่ถูกติดตั้งจาก Composer
ตัวอย่างไฟล์
หลังจากได้ไฟล์เรียบร้อยแล้ว ขั้นตอนถัดไปก็คือการสร้าง Web Sockets ที่ทำหน้าที่เป็น Server ซึ่งสามารถอ่านได้จากหัวข้อถัดไปในตอนที่ 3
|