public function testLeavesChannelCorrectly() { $package = (object) ['channel' => 'my-channel']; $chn = $this->channels->get('my-channel'); $this->assertTrue($chn->connectionExists($this->connection)); $this->command->run($this->connection, $package); $this->assertFalse($chn->connectionExists($this->connection)); }
/** * @inheritdoc */ public function run(ConnectionInterface $from, \stdClass $package) { $channelName = $package->channel; if (!$this->channels->exists($channelName)) { $chn = new Channel($channelName); $this->channels->add($chn); } $this->channels->get($channelName)->addConnection($from); }
public function testIsCaseInsensitive() { $chn = new Channel('my-channel'); $chn2 = new Channel('MY-CHANNEL'); $this->channels->add($chn, 'my-channel'); $this->channels->add($chn2, 'MY-CHANNEL'); $this->assertSame($chn, $this->channels->get('my-channel')); $this->assertSame($chn, $this->channels->get('MY-CHANNEL')); }
/** * @inheritdoc */ public function run(ConnectionInterface $from, \stdClass $package) { $channelName = $package->channel; $chn = $this->channels->get($channelName); if (!$chn) { return; } $chn->removeConnection($from); // remove the channel if it is empty. if (count($chn->getConnections()) === 0) { $this->channels->remove($channelName); } }
/** * @inheritdoc */ public function run(ConnectionInterface $from, \stdClass $package) { $channelName = $package->channel; $fromName = $package->from; $message = $package->message; // can't send to non-existent channels if (!$this->channels->exists($channelName)) { return; } $chn = $this->channels->get($channelName); // can't send to channels you're not apart of if (!$chn->connectionExists($from)) { return; } // this json-encoded string will be sent over the socket $outgoing = json_encode(['command' => $this->getName(), 'channel' => $channelName, 'message' => $message, 'from' => $fromName, 'timestamp' => time()]); foreach ($chn->getConnections() as $conn) { $conn->send($outgoing); } }