yiicod / yii2-socketio

46 stars 30 forks source link

How to connect to server from browser? #10

Closed shoxabbos closed 6 years ago

shoxabbos commented 6 years ago

How to connect to server from browser? here is my code

// Connect to socket.io
            var socket = io.connect('http://d.xcd.uz:1367/notifications');

            socket.on('connect', () => {
                console.log('hi');
            });

            socket.on('update_notification_count', function(data){
                console.log(data)
            });

            $("p").click(function () {
                socket.emit('mark_as_read_notification', {id: 10});
                console.log('clicked');
            });

But I'm getting errors on the console

polling-xhr.js:264 GET http://d.xcd.uz:1367/socket.io/?EIO=3&transport=polling&t=ME8OMmo 0 ()
lexxorlov commented 6 years ago

Check Asset, configuration, npm packages. I can not understand anything by your error.

shoxabbos commented 6 years ago

Client says that namespace invalid

http://prntscr.com/jlfyeo

this is my client code

http://prntscr.com/jlfytq

shoxabbos commented 6 years ago

Here is my console conf

  'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => $db,        

    ],
    'params' => $params,
    'controllerMap' => [
        'socketio' => [
            'class' => \yiicod\socketio\commands\WorkerCommand::class,
            'server' => 'd.xcd.uz:1367'
        ],
    ],

web config file

'components' => [
        'broadcastEvents' => [
            'class' => \yiicod\socketio\EventManager::class,
            'nsp' => 'some_unique_key',
            // Namespaces with events folders
            'namespaces' => [
                'app\events',
            ]
        ],

        'broadcastDriver' => [
            'class' => \yiicod\socketio\drivers\RedisDriver::class,
            'hostname' => 'localhost',
            'port' => 6379,
        ],
]

And I run this command from controller

\yiicod\socketio\Broadcast::emit(\app\events\CountEvent::name(), ['count' => 10]);

I copied all files from the example. What is the problem I do not understand? ((( Please help me 😢😢😢😢

shoxabbos commented 6 years ago

All daemons && workers are works! You can see it here http://prntscr.com/jlg24e

lexxorlov commented 6 years ago

Check this runtime/logs/exceptions.log. I think the problem on the server side. Right now i don't have any other suggestions

lexxorlov commented 6 years ago

one more, if you run daemon and after add event class, then you have to remove all daemons and stop all process and rerun pm2. You should do it every time when you add/update you event classes

shoxabbos commented 6 years ago

I solve it ))

lexxorlov commented 6 years ago

What was the problem?