├── .gitignore ├── test └── emit.php ├── composer.json ├── README.md └── src └── Emitter.php /.gitignore: -------------------------------------------------------------------------------- 1 | .buildpath 2 | .project 3 | .settings/org.eclipse.php.core.prefs -------------------------------------------------------------------------------- /test/emit.php: -------------------------------------------------------------------------------- 1 | in('chn1---------')->emit('newmsg', 'hello'); 8 | $emitter->emit('newmsg', 'aaaaaaaaa'); 9 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "workerman/phpsocket.io-emitter", 3 | "type" : "project", 4 | "homepage": "http://www.workerman.net", 5 | "license" : "MIT", 6 | "autoload": { 7 | "psr-4": {"": "./src"} 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # phpsocket.io-emitter 2 | 3 | ## Install 4 | composer require workerman/phpsocket.io-emitter 5 | 6 | ## example 7 | ```php 8 | $emitter = new Emitter(); 9 | $emitter->in('one room')->emit('newmsg', 'hello'); 10 | $emitter->emit('newmsg', array('type'=>'blog','content'=>'.....')); 11 | ``` 12 | -------------------------------------------------------------------------------- /src/Emitter.php: -------------------------------------------------------------------------------- 1 | _remoteIp = $ip; 21 | $this->_remotePort = $port; 22 | $this->_context = $context; 23 | $this->connect(); 24 | } 25 | 26 | protected function connect() 27 | { 28 | if(is_array($this->_context)) 29 | { 30 | $context = stream_context_create($this->_context); 31 | $this->_client = stream_socket_client("tcp://{$this->_remoteIp}:{$this->_remotePort}", $errno, $errmsg, 3, STREAM_CLIENT_CONNECT, $context); 32 | }else{ 33 | $this->_client = stream_socket_client("tcp://{$this->_remoteIp}:{$this->_remotePort}", $errno, $errmsg, 3); 34 | } 35 | 36 | if(!$this->_client) 37 | { 38 | throw new \Exception($errmsg); 39 | } 40 | } 41 | 42 | public function __get($name) 43 | { 44 | if($name === 'broadcast') 45 | { 46 | $this->_flags['broadcast'] = true; 47 | return $this; 48 | } 49 | return null; 50 | } 51 | 52 | public function to($name) 53 | { 54 | if(!isset($this->_rooms[$name])) 55 | { 56 | $this->_rooms[$name] = $name; 57 | } 58 | return $this; 59 | } 60 | 61 | public function in($name) 62 | { 63 | return $this->to($name); 64 | } 65 | 66 | public function emit($ev) 67 | { 68 | if(feof($this->_client)) 69 | { 70 | $this->connect(); 71 | } 72 | 73 | $args = func_get_args(); 74 | 75 | $parserType = 2;// Parser::EVENT 76 | 77 | $packet = array('type'=> $parserType, 'data'=> $args, 'nsp'=>'/' ); 78 | 79 | $buffer = serialize(array( 80 | 'type' => 'publish', 81 | 'channels'=>array($this->_key), 82 | 'data' => array('-', $packet, 83 | array( 84 | 'rooms' => $this->_rooms, 85 | 'flags' => $this->_flags 86 | ) 87 | ) 88 | ) 89 | ); 90 | 91 | $buffer = pack('N', strlen($buffer)+4).$buffer; 92 | 93 | fwrite($this->_client, $buffer); 94 | 95 | $this->_rooms = array(); 96 | $this->_flags = array(); 97 | return $this; 98 | } 99 | } 100 | --------------------------------------------------------------------------------