jgswift/qtcp

Web socket server/client using php and javascript

dev-master 2014-12-08 00:49 UTC

This package is not auto-updated.

Last update: 2024-11-05 04:39:44 UTC


README

Web socket client/server using php and javascript

Build Status

Installation

Install via composer:

php composer.phar require jgswift/qtcp:dev-master

Dependency

Description

qtcp is an experimental abstraction layer for client/server applications using websockets

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

The sample stream serves as a conceptual prototype to demonstrate the most basic functionality

Server

Configure

First, configure the host and port by editing the config.js

If you intend to run the client and server together on the same box, a likely configuration may be the following

var SampleStream = {
    host: 'localhost',
    port: 8081
};

Run

In terminal or via SSH, navigate to the directory qtcp is located in and start the server

$ cd vendor/jgswift/qtcp/
$ php tests/Examples/SampleStream/Server.php localhost:8081

The server will start and you will see

Starting server..
Server started.

Code

$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.

A sample application will appear and click the button to send your first packet

Code

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

The currency stream example simulates a currency index which concurrently updates all clients with price changes.

Server

Configure

Like above, configure the host and port by editing the config.js

If you intend to run the client and server together on the same box, a likely configuration may be the following

var CurrencyStream = {
    host: 'localhost',
    port: 8081
};

Run

In terminal or via SSH, navigate to the directory qtcp is located in and start the server

$ cd vendor/jgswift/qtcp/
$ php tests/Examples/CurrencyStream/Server.php

Alternatively, you may specify the host/port

$ php tests/Examples/CurrencyStream/Server.php 0.0.0.0:8081

The server will start and you will see

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.

The price streaming application will list a currency index. Check any boxes on the left to initiate streaming.