Updated example, slight Origin order change in app

This commit is contained in:
Chris Boden 2013-07-20 22:03:46 -04:00
parent f50af83fa7
commit eb77cf139e
2 changed files with 15 additions and 7 deletions

View File

@ -30,20 +30,21 @@ Need help? Have a question? Want to provide feedback? Write a message on the
--- ---
###A quick server example ###A quick example
```php ```php
<?php <?php
use Ratchet\MessageComponentInterface; use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
// Make sure composer dependencies have been installed
require __DIR__ . '/vendor/autoload.php'; require __DIR__ . '/vendor/autoload.php';
/** /**
* chat.php * chat.php
* Send any incoming messages to all connected clients (except sender) * Send any incoming messages to all connected clients (except sender)
*/ */
class Chat implements MessageComponentInterface { class MyChat implements MessageComponentInterface {
protected $clients; protected $clients;
public function __construct() { public function __construct() {
@ -72,10 +73,17 @@ class Chat implements MessageComponentInterface {
} }
// Run the server application through the WebSocket protocol on port 8080 // Run the server application through the WebSocket protocol on port 8080
$app = new Ratchet\App('example.com', 8080); $app = new Ratchet\App('localhost', 8080);
$app->route('/chat', new Chat); $app->route('/chat', new MyChat);
$app->route('/echo', new Ratchet\Server\EchoServer); $app->route('/echo', new Ratchet\Server\EchoServer, array(*));
$app->run(); $app->run();
``` ```
$ php chat.php $ php chat.php
```javascript
// Then some JavaScript in the browser:
var conn = new WebSocket('ws://localhost/echo');
conn.onmessage = function(e) { console.log(e.data); };
conn.send('Hello Me!');
```

View File

@ -97,10 +97,10 @@ class App {
$httpHost = $httpHost ?: $this->httpHost; $httpHost = $httpHost ?: $this->httpHost;
$allowedOrigins = array_values($allowedOrigins);
if (0 === count($allowedOrigins)) { if (0 === count($allowedOrigins)) {
$allowedOrigins[] = $httpHost; $allowedOrigins[] = $httpHost;
} }
$allowedOrigins = array_values($allowedOrigins);
if ('*' !== $allowedOrigins[0]) { if ('*' !== $allowedOrigins[0]) {
$decorated = new OriginCheck($decorated, $allowedOrigins); $decorated = new OriginCheck($decorated, $allowedOrigins);
} }