swoole实现一个socketio示例代码
代码语言:phpcli
所属分类:通讯
代码描述:swoole实现一个socketio示例代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<?php /** * packet types * 0 open * Sent from the server when a new transport is opened (recheck) * 1 close * Request the close of this transport but does not shutdown the connection itself. * 2 ping * Sent by the client. Server should answer with a pong packet containing the same data * 3 pong * Sent by the server to respond to ping packets. * 4 message * actual message, client and server should call their callbacks with the data. * 5 upgrade * Before engine.io switches a transport, it tests, if server and client can communicate over this transport. If this * test succeed, the client sends an upgrade packets which requests the server to flush its cache on the old transport * and switch to the new transport. * 6 noop * A noop packet. Used primarily to force a poll cycle when an incoming websocket connection is received. * packet data types * Packet#CONNECT (0) * Packet#DISCONNECT (1) * Packet#EVENT (2) * Packet#ACK (3) * Packet#ERROR (4) * Packet#BINARY_EVENT (5) * Packet#BINARY_ACK (6) * basic format => $socket->emit("message", "hello world"); * => sprintf('%d%d%s', $packetType, $packetDataType, json_encode([$event, $data])) * => 42["message", "hello world"] */ class SocketIOParser { private static $instance; public $id; protected $server; protected $events = []; private $ackId; private function __construct() {} public static function getInstance() { return self::$instance ?: (self::$instance = new self); } /** * register socketio event * @param $event * @param Closure $callback */ public function on($event, Closure $callback) { if (is_string($event)) { $this->events[$event] = $callback; } } /** * listen server event * @param $server */ public function bindEngine($server) { $server->on('Open', [$this, 'onOpen']); $server->on('Message', [$this, 'onMessage']); $server->on('Close', [$this, 'onClose']); } public function onOpen($server, $request) { $this->server = $server; $this->id = $request->.........完整代码请登录后点击上方下载按钮下载查看
网友评论0