qtcp
Web socket client/server using php and javascript, (*1)
, (*2)
Installation
Install via composer:, (*3)
php composer.phar require jgswift/qtcp:dev-master
Dependency
Description
qtcp is an experimental abstraction layer for client/server applications using websockets, (*4)
Usage
$app = new qtcp\Network\Application(['0.0.0.0',8081]);
$app->attach('connect',function($client) {
$client->attach('event', function($client, $e) {
/* do something with event */
});
$client->attach('disconnect', function() {
/* teardown client here */
});
});
$app->run();
Examples
Sample Stream
tests/Examples/SampleStream, (*5)
The sample stream serves as a conceptual prototype to demonstrate the most basic functionality, (*6)
Server
Configure, (*7)
First, configure the host and port by editing the config.js, (*8)
If you intend to run the client and server together on the same box, a likely configuration may be the following, (*9)
var SampleStream = {
host: 'localhost',
port: 8081
};
Run, (*10)
In terminal or via SSH, navigate to the directory qtcp is located in and start the server, (*11)
$ cd vendor/jgswift/qtcp/
$ php tests/Examples/SampleStream/Server.php localhost:8081
The server will start and you will see, (*12)
Starting server..
Server started.
Code, (*13)
$app = new qtcp\Network\Application([$host,$port]);
$app->attach('connect',function($client) {
$client->attach('event', function($client, $e) {
/* send reply */
$client->send(new qtcp\Network\Packet('event',['hello world!']));
});
$client->attach('disconnect', function() {
/* do something with disconnect */
});
});
$app->run();
Client
Open a web browser and navigate to http://localhost/your_project_directory/vendor/jgswift/qtcp/tests/Examples/SampleStream
.
Note: Modify path if qtcp is in a different directory., (*14)
A sample application will appear and click the button to send your first packet, (*15)
Code, (*16)
qtcp.network.client = new qtcp.client(
"body",
new qtcp.stream(
new qtcp.resource(SampleStream.host,SampleStream.port)
)
);
// attach packet processor for event packet
qtcp.network.client.attach("event",function(data) {
$("#response").html(data[0]);
});
// connect to server
qtcp.network.client.connect();
// send event packet with some dummy data
$('input').on('click',function() {
qtcp.network.client.send(new qtcp.network.packet("event"),{var1:"test"});
});
Currency Stream
tests/Examples/CurrencyStream, (*17)
The currency stream example simulates a currency index which concurrently updates all clients with price changes., (*18)
Server
Configure, (*19)
Like above, configure the host and port by editing the config.js, (*20)
If you intend to run the client and server together on the same box, a likely configuration may be the following, (*21)
var CurrencyStream = {
host: 'localhost',
port: 8081
};
Run, (*22)
In terminal or via SSH, navigate to the directory qtcp is located in and start the server, (*23)
$ cd vendor/jgswift/qtcp/
$ php tests/Examples/CurrencyStream/Server.php
Alternatively, you may specify the host/port, (*24)
$ php tests/Examples/CurrencyStream/Server.php 0.0.0.0:8081
The server will start and you will see, (*25)
Starting server..
Server started.
Client
Open a web browser and navigate to http://localhost/your_project_directory/vendor/jgswift/qtcp/tests/Examples/CurrencyStream
.
Note: Modify path if qtcp is in a different directory., (*26)
The price streaming application will list a currency index. Check any boxes on the left to initiate streaming., (*27)