Skip to content

Instantly share code, notes, and snippets.

@kinncj
Last active December 29, 2015 15:59
Show Gist options
  • Save kinncj/7694779 to your computer and use it in GitHub Desktop.
Save kinncj/7694779 to your computer and use it in GitHub Desktop.
Socketo.me implementation

HHVM Support

HHVM Support

hhvm bin/chat-server.php

Open some JS Console, as Google Chrome console.

var conn = new WebSocket('ws://127.0.0.1:8080');
conn.onopen = function(e) {
    console.log("Connection established!");
};

conn.onmessage = function(e) {
    console.log(e.data);
};

Open another JS Console:

var conn = new WebSocket('ws://127.0.0.1:8080');
conn.onopen = function(e) {
    console.log("Connection established!");
};

conn.onmessage = function(e) {
    console.log(e.data);
};

conn.send('Hello World!');

Go back to the previous console and check the results :-)

<?php
//bin/chat-server.php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
require dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080
);
$server->run();
<?php
//src/WsChatter/Chat.php
namespace WsChatter;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg) {
$numRecv = count($this->clients) - 1;
echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
, $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
foreach ($this->clients as $client) {
if ($from !== $client) {
// The sender is not the receiver, send to each client connected
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn) {
// The connection is closed, remove it, as we can no longer send it messages
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
{
"_comment": "In this case, the require is using the defualt Ratchet package, should change to use the one in README.md file",
"autoload": {
"psr-0": {
"WsChatter": "src"
}
},
"require": {
"cboden/Ratchet": "0.3.*"
}
}
var conn = new WebSocket('ws://127.0.0.1:8080');
conn.onopen = function(e) {
console.log("Connection established!");
};
conn.onmessage = function(e) {
console.log(e.data);
};
conn.send('Hello World!');
@SergeSysoev
Copy link

Hi! How can I send message to only one client? Or how I can get access to $_SESSION and $_GET?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment