WebSocket服务器的PHP实现 - PHP WebSocket
jopen
12年前
PHP WebSocket是一个简单的 WebSocket服务器的PHP实现,遵循WebSocket RFC的draft75和draft76规范。支持Flash Socket Policy请求。整个服务器采用模块化设计,非常易于扩展。
服务器实现:
class EchoApplication extends Application { private $clients = array(); public function onConnect($client) { $this->clients[] = $client; } public function onDisconnect($client) { $key = array_search($client, $this->clients); if ($key) { unset($this->clients[$key]); } } public function onData($data, $client) { foreach ($this->clients as $sendto) { $sendto->send($data); } } }
客户端代码示例:
var server = new WebPush('ws://localhost:8000/echo'); server.bind('open', function() { // Connection openend... server.send("Hello, I'm there!"); }); server.bind('close', function() { // Connection closed... }); server.bind('message', function(data) { // Data received });