Copy-pasted the README.md
quick example of ratchetphp/Ratchet
5012dc9545 (a-quick-example)
This commit is contained in:
parent
931b2df563
commit
0193f05143
44
website/chat.php
Normal file
44
website/chat.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
use Ratchet\MessageComponentInterface;
|
||||
use Ratchet\ConnectionInterface;
|
||||
|
||||
// Make sure composer dependencies have been installed
|
||||
require __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
/**
|
||||
* chat.php
|
||||
* Send any incoming messages to all connected clients (except sender)
|
||||
*/
|
||||
class MyChat implements MessageComponentInterface {
|
||||
protected $clients;
|
||||
|
||||
public function __construct() {
|
||||
$this->clients = new \SplObjectStorage;
|
||||
}
|
||||
|
||||
public function onOpen(ConnectionInterface $conn) {
|
||||
$this->clients->attach($conn);
|
||||
}
|
||||
|
||||
public function onMessage(ConnectionInterface $from, $msg) {
|
||||
foreach ($this->clients as $client) {
|
||||
if ($from != $client) {
|
||||
$client->send($msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function onClose(ConnectionInterface $conn) {
|
||||
$this->clients->detach($conn);
|
||||
}
|
||||
|
||||
public function onError(ConnectionInterface $conn, \Exception $e) {
|
||||
$conn->close();
|
||||
}
|
||||
}
|
||||
|
||||
// Run the server application through the WebSocket protocol on port 8080
|
||||
$app = new Ratchet\App('localhost', 8080);
|
||||
$app->route('/chat', new MyChat, array('*'));
|
||||
$app->route('/echo', new Ratchet\Server\EchoServer, array('*'));
|
||||
$app->run();
|
5
website/composer.json
Normal file
5
website/composer.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"require": {
|
||||
"cboden/ratchet": "^0.4.4"
|
||||
}
|
||||
}
|
1411
website/composer.lock
generated
Normal file
1411
website/composer.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -11,3 +11,10 @@ See <?php echoUrl('https://gitea.lemnoslife.com/Benjamin_Loison/YouTube_captions
|
||||
|
||||
<input type="text" pattern="[A-Za-z0-9]+" placeholder="Your alphanumeric search"></input>
|
||||
<button>Search</button>
|
||||
|
||||
<script>
|
||||
// Then some JavaScript in the browser:
|
||||
var conn = new WebSocket('ws://localhost:8080/echo');
|
||||
conn.onmessage = function(e) { console.log(e.data); };
|
||||
conn.onopen = function(e) { conn.send('Hello Me!'); };
|
||||
</script>
|
||||
|
Loading…
Reference in New Issue
Block a user