Download the PHP package consik/yii2-websocket without Composer
On this page you can find all versions of the php package consik/yii2-websocket. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package yii2-websocket
Yii2 WebSocketServer
Used Ratchet
Installation
The preferred way to install this extension is through composer.
Either run
or add
WebSocketServer class description
Properties
-
- Port number for websocket server
-
- Close connection or not when error occurs with it
-
- Check client's messages for commands or not
-
- IOServer object
-
- Storage of connected clients
Methods
Events
- EVENT_WEBSOCKET_OPEN
Class yii\base\Event - Triggered when binding is successfully completed
- EVENT_WEBSOCKET_CLOSE
Class yii\base\Event - Triggered when socket listening is closed
- EVENT_WEBSOCKET_OPEN_ERROR
Class events\ExceptionEvent - Triggered when throwed Exception on binding socket
- EVENT_CLIENT_CONNECTED
Class events\WSClientEvent - Triggered when client connected to the server
- EVENT_CLIENT_DISCONNECTED
Class events\WSClientEvent - Triggered when client close connection with server
- EVENT_CLIENT_ERROR
Class events\WSClientErrorEvent - Triggered when an error occurs on a Connection
- EVENT_CLIENT_MESSAGE
Class events\WSClientMessageEvent - Triggered when message recieved from client
- EVENT_CLIENT_RUN_COMMAND
Class events\WSClientCommandEvent - Triggered when controller starts user's command
- EVENT_CLIENT_END_COMMAND
Class events\WSClientCommandEvent - Triggered when controller finished user's command
Examples
Simple echo server
Create your server class based on WebSocketServer. For example :
Create yii2 console controller for starting server:
Start your server using console:
php yii server/start
Now let's check our server via js connection:
Console result must be:
Connection established!
Hey!
Response:Hey!
Handle server starting success and error events
Now we try handle socket binding error and open it on other port, when error occurs;
Create yii2 console controller for starting server:
Start your server using console command:
php yii server/start
Server console result must be:
Error opening port 80
Server started at port 81
Recieving client commands
You can implement methods that will be runned after some of user messages automatically;
Server class :
Run the server like in examples above
Check connection and command working by js script:
Console result must be:
ping
Response:Pong
Chat example
In the end let's make simple chat with sending messages and function to change username;
Code without comments, try to understand it by youself ;)
-
Server class :
-
Simple html form :
- JS code for chat with jQuery:
Enjoy ;)