public function generateTocken() { $tckID = uniqid(); $fsnode = new FSNode(); $fsnode->tocken = $tckID; $fsnode->user_id = $this->id; $fsnode->save(); return $tckID; }
/** * Prepares a new WebSocket server on a specified host & port. * * @param string $tcpid * * @return YnievesDotNet\FourStream\FourStream\FourStreamServer */ public function start($tcpid) { $oldNode = FSNode::all(); echo "Closing old nodes", "\n"; foreach ($oldNode as $node) { $node->delete(); } $this->server = new Websocket(new Socket($tcpid)); $this->server->on('open', function (Bucket $bucket) { Event::fire(new Events\ConnectionOpen($bucket)); }); $this->server->on('message', function (Bucket $bucket) { Event::fire(new Events\MessageReceived($bucket)); }); $this->server->on('binary-message', function (Bucket $bucket) { Event::fire(new Events\BinaryMessageReceived($bucket)); }); $this->server->on('ping', function (Bucket $bucket) { Event::fire(new Events\PingReceived($bucket)); }); $this->server->on('error', function (Bucket $bucket) { Event::fire(new Events\ErrorGenerated($bucket)); }); $this->server->on('close', function (Bucket $bucket) { Event::fire(new Events\ConnectionClose($bucket)); }); return $this; }
/** * Handle the event. * * @param Received $event * @return void */ public function handle(Received $event) { $data = $event->bucket->getData(); $node = $event->bucket->getSource()->getConnection()->getCurrentNode(); $data = json_decode($data['message']); $nodes = $event->bucket->getSource()->getConnection()->getNodes(); $fsnode = FSNode::where('node_id', $node->getId())->first(); if ($data->type == "auth") { $tck = $data->data; $fsnode = FSNode::where('tocken', $tck)->first(); $fsnode->node_id = $node->getId(); $fsnode->save(); if ($data->tag != "" && $fsnode) { $fsnode->fstags()->create(['tag' => $data->tag]); } return; } $router = app('fs.router'); if (isset($router->actions[$data->type])) { $router->dispatch($event); } if (config('app.debug')) { echo "> Message Received. \n"; } }
/** * @param $type * @param $data * @param $id */ public function sendUserID($type, $data, $id) { $nodes = FSNode::where("user_id", $id)->get(); foreach ($nodes as $node) { self::send($type, $data, $node->node_id); } }
/** * Handle the Event. * * @param Close $event * @return void */ public function handle(Close $event) { $node = $event->bucket->getSource()->getConnection()->getCurrentNode(); $data = $event->bucket->getData(); FSNode::where('node_id', $node->getId())->delete(); if (config('app.debug')) { echo "< Connection Closed: " . $node->getId() . " code: " . $data['code'] . " reason: " . $data['reason'] . "\n"; } }