krissss / yii2-web-msg-sender

Yii2 web msg sender with workerman
3 stars 1 forks source link

Unable to POST data #2

Closed i-internet closed 3 years ago

i-internet commented 3 years ago

First of all, thank you so much for this great work of yours.

I have successfully configured this module but unable to push POST data using CURL in Service.php

    $this->socketIo->on('workerStart', function () {
            // 监听一个http端口
            echo "I am listeining";
            $innerHttpWorker = new Worker($this->webMsgSender->getPushApiServerUrl());
            // 当http客户端发来数据时触发
            $innerHttpWorker->onMessage = function (TcpConnection $httpConnection) {
                $uidConnectionMap = $this->globalData['uidConnectMap'];
                $requestParams = $_POST ?: $_GET;
                // 推送数据的url格式 type=publish&to=uid&content=xxxx
                $model = new ClientSendModel(['socketIo' => $this->socketIo]);
                echo 'requestParams';
                echo PHP_EOL;
                print_R($requestParams); //this is coming blank in console
                echo PHP_EOL;
                echo 'model';
i-internet commented 3 years ago

web.php

 'components' => [
        'web-msg-sender' => [
            'class' => 'kriss\webMsgSender\WebMsgSender',
            'pushApiClientHost' => 'localhost',
            'logCategory' => 'webMsgSender',
        ],

console.php

 'controllerMap' => [
        'web-msg-sender-service' => [
            'class' => \kriss\webMsgSender\ServiceController::class,
        ],
    ],
 'components' => [
        'web-msg-sender' => [
            'class' => 'kriss\webMsgSender\WebMsgSender',
            'pushApiClientHost' => 'localhost',
            'logCategory' => 'webMsgSender',
        ],

inside controller

 public function actionBla(){
         WebMsgSender::getComponent()->getSender()->sendDanger('testing',1191);
        die('DIE');
    }
krissss commented 3 years ago

Please see log in app.log if you dont configed log config.

i-internet commented 3 years ago

Please see log in app.log if you dont configed log config.

 $innerHttpWorker->onMessage = function (TcpConnection $httpConnection) {
                $uidConnectionMap = $this->globalData['uidConnectMap'];
                $requestParams = $_POST ?: $_GET;

The problem is $requestParams is coming blank when i hit the URL or your method .

The logs for the console app ? for web app it already there

i-internet commented 3 years ago

There is no log for this

krissss commented 3 years ago

Is $innerHttpWorker->onMessage function triggered?

Use log not echo to debug.

already log written by this, you can see is this log exist https://github.com/krissss/yii2-web-msg-sender/blob/9b3ee9e1cf77966e9c83ab93fb5eb112b7389ed4/src/ServerSender.php#L49

i-internet commented 3 years ago

Is $innerHttpWorker->onMessage function triggered?

Use log not echo to debug.

already log written by this, you can see is this log exist https://github.com/krissss/yii2-web-msg-sender/blob/9b3ee9e1cf77966e9c83ab93fb5eb112b7389ed4/src/ServerSender.php#L49

Yes $innerHttpWorker->onMessage function triggered successfully .

Changes in your file for logging somehow console logs not working even after setup correctly :-

in console.php

  'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => [
                        'error',
                        'warning'
                    ]
                ]
            ]
        ],
  $innerHttpWorker->onMessage = function (TcpConnection $httpConnection,$data) {
                echo "Someone hit the URL";
                $uidConnectionMap = $this->globalData['uidConnectMap'];
                $requestParams = $_POST ?: $_GET;
                // 推送数据的url格式 type=publish&to=uid&content=xxxx
                $model = new ClientSendModel(['socketIo' => $this->socketIo]);
                echo 'requestParams';
                echo PHP_EOL;
                print_R($requestParams);

This is how I start from console

$ php console.php web-msg-sender-service/start
----------------------- WORKERMAN -----------------------------
Workerman version:4.0.17          PHP version:7.3.15
------------------------ WORKERS -------------------------------
worker               listen                              processes status
PHPSocketIO          socketIO://0.0.0.0:2120             1         [ok]
I am listeining to http://0.0.0.0:2121/

After I hit this URL http://localhost:2121/?type=publish&to=1191&content=checking or run this function from action WebMsgSender::getComponent()->getSender()->sendDanger('checking',1191);

In console, I get this

Someone hit the URL
requestParams
Array
(
)
i-internet commented 3 years ago

Logs are working and this is what is coming in that

2020-12-30 05:34:09 [-][-][-][error][application] INSIDE TEST
2020-12-30 05:34:09 [-][-][-][error][application] {"pushApiUrl":"http://localhost:2121/","data":{"type":"publish","content":"<div class=\"alert alert-info alert-dismissible\" role=\"alert\">\n  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>\n  2020-12-30 05:34:09 hie whats up\n</div>","to":1191}}
2020-12-30 05:34:09 [-][-][-][error][application] {"result":"fail"}
2020-12-30 05:34:09 [-][-][-][info][application] $_GET = []

$_POST = []

$_FILES = []

$_COOKIE = []
krissss commented 3 years ago

I think everything you write is right. I guess this caused by workerman 4.x version. I dont test this version.

I will test this later.

i-internet commented 3 years ago

Hi Kudos , I figured it out

     $innerHttpWorker->onMessage = function (TcpConnection $httpConnection,$request) {
                $uidConnectionMap = $this->globalData['uidConnectMap'];
                $requestParams = $_POST ?: $_GET;
                $this->webMsgSender->logger('requestParams');
                $this->webMsgSender->logger($requestParams);
                $this->webMsgSender->logger($_SERVER ??'EMPTY SERVER VARIABLE');
                $this->webMsgSender->logger('workerStart_data');
                $this->webMsgSender->logger('TcpConnection');
                $this->webMsgSender->logger($request->post());
                echo "I AM HERE";
                print_R($request->post());
 $innerHttpWorker->onMessage = function (TcpConnection $httpConnection,$request) {

Now $request->post() contains the data

Will you update this please or I should do it manually in my code ?

krissss commented 3 years ago

I will change later. Maybe some hours later.

tks

krissss commented 3 years ago

v1.2 has released

i-internet commented 3 years ago

Thank you I am stuck on a live environment and ports are not working with SSL throwing error:-

 https://mydoamin.com:2120/socket.io/?EIO=3&transport=polling&t=NQpMyRI net::ERR_TIMED_OUT
i.create @ socket.io.js?v=1609310849:7

Btw thanks again

krissss commented 3 years ago

Use Nginx for proxy forwarding to solve ssl error

i-internet commented 3 years ago

Use Nginx for proxy forwarding to solve ssl error

I am far a head of everything that means i am struck, moving apache2 to nginx will take time

krissss commented 3 years ago

Just use master version for test, I pushed code for support socketIO options for config ssl. Not release because I dont test this.

config ssl with:

 'components' => [
        'web-msg-sender' => [
            'class' => 'kriss\webMsgSender\WebMsgSender',
            'pushApiClientHost' => 'localhost',
            'logCategory' => 'webMsgSender',
            'socketOpts' => [  //<- add this and change ssl!!!
                'ssl' => [
                    'local_cert'  => '/your/path/of/server.pem',
                    'local_pk'    => '/your/path/of/server.key',
                    'verify_peer' => false,
                ],
            ],
        ],
i-internet commented 3 years ago

Just use master version for test, I pushed code for support socketIO options for config ssl. Not release because I dont test this.

config ssl with:

 'components' => [
        'web-msg-sender' => [
            'class' => 'kriss\webMsgSender\WebMsgSender',
            'pushApiClientHost' => 'localhost',
            'logCategory' => 'webMsgSender',
            'socketOpts' => [  //<- add this and change ssl!!!
                'ssl' => [
                    'local_cert'  => '/your/path/of/server.pem',
                    'local_pk'    => '/your/path/of/server.key',
                    'verify_peer' => false,
                ],
            ],
        ],

ok this I will test but the first config should set here somewhere as it is giving an initial error :-

https://github.com/krissss/yii2-web-msg-sender/blob/master/src/ClientReceiveWidget.php
 public function run()
    {
        echo Html::tag('div', '', ['id' => $this->id]);
        $webMsgSender = WebMsgSender::getComponent();

        SocketIoAsset::register($this->view);
        $js = <<<JS
        var uid = '{$this->uid}';
        var containerEl = document.getElementById('{$this->id}');
        var socket = io('{$webMsgSender->getReceiveSocketUrl()}');
        socket.on('connect', function(){
          socket.emit('login', uid);
        });
        socket.on('new_msg', function(msg) {
          containerEl.innerHTML += msg;
        });
JS;
        $this->view->registerJs($js);
    }

if you see var socket = io('{$webMsgSender->getReceiveSocketUrl()}'); it is just getting the URL not opts

krissss commented 3 years ago

socketOpts is for server SocketIO, not client

i-internet commented 3 years ago

socketOpts is for server SocketIO, not client ok testing above code but client is giving this error socket.io.js?v=1609310849:7 GET https://myDoamin.com:2120/socket.io/?EIO=3&transport=polling&t=NQpU3PA net::ERR_CONNECTION_RESET

krissss commented 3 years ago

Be sure ssl file matched domain?

i-internet commented 3 years ago

$pushApiUrl = $this->webMsgSender->getPushApiClientUrl(); = https://0.0.0.0:2121/ console.php & web.php

 'web-msg-sender' => [
            'class' => 'app\base\socketIO\WebMsgSender',
            'pushApiClientHost' => '0.0.0.0',
            'logCategory' => 'application',
            'logType' => 'error'
        ],
krissss commented 3 years ago

Put all your 'web-msg-sender' config(change some security words). If this is all your config, its wrong

 'web-msg-sender' => [
            'class' => 'app\base\socketIO\WebMsgSender',
            'pushApiClientHost' => '0.0.0.0',
            'logCategory' => 'application',
            'logType' => 'error'
        ],
krissss commented 3 years ago

put your app\base\socketIO\WebMsgSender where changed

i-internet commented 3 years ago

our

Put all your 'web-msg-sender' config(change some security words). If this is all your config, its wrong

 'web-msg-sender' => [
            'class' => 'app\base\socketIO\WebMsgSender',
            'pushApiClientHost' => '0.0.0.0',
            'logCategory' => 'application',
            'logType' => 'error'
        ],

Remove my configs now Oh I did not know this as it was working fantastic in localhost . I can donate for your great work if you could help, please.

krissss commented 3 years ago

I have tested, SSL can used with socketOpts config. and released v1.4.

SSL has add in README

Don't forget to open the port which you use on the server!!

i-internet commented 3 years ago

I have tested, SSL can used with socketOpts config. and released v1.4.

SSL has add in README

Don't forget to open the port which you use on the server!!

Sure thanks I will setup ngnix server and update . really appreciate