Example #1
0
    /**
     * @param bool $insert
     * @param array $changedAttributes
     * @throws \WebSocket\BadOpcodeException
     */
    public function afterSave($insert, $changedAttributes)
    {
        Yii::trace('try to send a call information to site ' . $this->paramint, 'app\\modules\\event\\PhoneEvent::afterSave');
        //initialize the response object
        $res = [];
        $res['type'] = 'call';
        $res['site'] = $this->paramint;
        $WSServer = \Yii::$app->params['websocketServerProtocol'] . \Yii::$app->params['clientCallWebsocketServerHost'] . ':' . \Yii::$app->params['clientCallWebsocketServerPort'];
        $client = new Client($WSServer);
        $timeinfo = Yii::$app->formatter->asDateTime($this->updated_at);
        $html = <<<EOF
\t<div style="width:250px" class="event_notification">
\t\t<div class="{$this->action}">
\t\t\t<i class="fa fa-phone"></i> {$this->action}
\t\t</div>
\t\t<small>{$timeinfo}</small><br>
\t\t<a href="#">
\t\t\tYou have an {$this->mod_table} call from {$this->paramtext}.
\t\t</a>
\t</div>
EOF;
        $res['data'] = $html;
        try {
            $client->send(Json::encode($res));
        } catch (ConnectionException $e) {
            echo "\n" . microtime(true) . " Client died: {$e}\n";
        }
        return parent::afterSave($insert, $changedAttributes);
    }