Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
# Ratchet
2
 
3
[![Build Status](https://secure.travis-ci.org/ratchetphp/Ratchet.png?branch=master)](http://travis-ci.org/ratchetphp/Ratchet)
4
[![Autobahn Testsuite](https://img.shields.io/badge/Autobahn-passing-brightgreen.svg)](http://socketo.me/reports/ab/index.html)
5
[![Latest Stable Version](https://poser.pugx.org/cboden/ratchet/v/stable.png)](https://packagist.org/packages/cboden/ratchet)
6
 
7
A PHP library for asynchronously serving WebSockets.
8
Build up your application through simple interfaces and re-use your application without changing any of its code just by combining different components.
9
 
10
## Requirements
11
 
12
Shell access is required and root access is recommended.
13
To avoid proxy/firewall blockage it's recommended WebSockets are requested on port 80 or 443 (SSL), which requires root access.
14
In order to do this, along with your sync web stack, you can either use a reverse proxy or two separate machines.
15
You can find more details in the [server conf docs](http://socketo.me/docs/deploy#server_configuration).
16
 
17
### Documentation
18
 
19
User and API documentation is available on Ratchet's website: http://socketo.me
20
 
21
See https://github.com/cboden/Ratchet-examples for some out-of-the-box working demos using Ratchet.
22
 
23
Need help?  Have a question?  Want to provide feedback?  Write a message on the [Google Groups Mailing List](https://groups.google.com/forum/#!forum/ratchet-php).
24
 
25
---
26
 
27
### A quick example
28
 
29
```php
30
<?php
31
use Ratchet\MessageComponentInterface;
32
use Ratchet\ConnectionInterface;
33
 
34
    // Make sure composer dependencies have been installed
35
    require __DIR__ . '/vendor/autoload.php';
36
 
37
/**
38
 * chat.php
39
 * Send any incoming messages to all connected clients (except sender)
40
 */
41
class MyChat implements MessageComponentInterface {
42
    protected $clients;
43
 
44
    public function __construct() {
45
        $this->clients = new \SplObjectStorage;
46
    }
47
 
48
    public function onOpen(ConnectionInterface $conn) {
49
        $this->clients->attach($conn);
50
    }
51
 
52
    public function onMessage(ConnectionInterface $from, $msg) {
53
        foreach ($this->clients as $client) {
54
            if ($from != $client) {
55
                $client->send($msg);
56
            }
57
        }
58
    }
59
 
60
    public function onClose(ConnectionInterface $conn) {
61
        $this->clients->detach($conn);
62
    }
63
 
64
    public function onError(ConnectionInterface $conn, \Exception $e) {
65
        $conn->close();
66
    }
67
}
68
 
69
    // Run the server application through the WebSocket protocol on port 8080
70
    $app = new Ratchet\App('localhost', 8080);
71
    $app->route('/chat', new MyChat, array('*'));
72
    $app->route('/echo', new Ratchet\Server\EchoServer, array('*'));
73
    $app->run();
74
```
75
 
76
    $ php chat.php
77
 
78
```javascript
79
    // Then some JavaScript in the browser:
80
    var conn = new WebSocket('ws://localhost:8080/echo');
81
    conn.onmessage = function(e) { console.log(e.data); };
82
    conn.onopen = function(e) { conn.send('Hello Me!'); };
83
```