yii-node-socket is not working for me

2019-09-17 08:21发布

问题:

Recently I have been trying to integrate nodejs with my existing project to make sure of live updating of feeds. Thus, I used the following yii plugin and followed the instruction:

https://github.com/oncesk/yii-node-socket

The host and port are: bigcat,3001 respectively and I have configured them in the console.php and main.php as mentioned. I started the node server and the log indicated that it was listening to:

Listening bigcat:3001
Set origin: bigcat:*

At my client side I have the following codes (i created it as an external javascript):

// create object and connect to web socket server
var socket = new YiiNodeSocket(); 

socket.on('event.example', function () {
    console.log('*****Trigger test.event, is a global event, for all clients*****');
});

socket.onConnect(function () {
    socket.room('example').join(function (isConnected, numberOfRoomClients) {
        if (isConnected) {
            // catch test.event only for this rooom
            console.log('***Joined into room example: members count [' + numberOfRoomClients + ']*****');
            this.on('example.room.event', function (data) {
                 console.log('****WORKING****');
            });
        }
    });
});

My console records that the client is successfully connected.An at homeController test method, I have the following code:

 $event = Yii::app()->nodeSocket->getFrameFactory()->createEventFrame();
 $event->setRoom('example');
 $event->setEventName('example.room.event');
 $event['type_string'] = 'hello world';
 $event['type_array'] = array(1, 2, 3);
 $event['type_object'] = array('one' => 1, 'two' => 2);
 $event['type_bool'] = true;
 $event['type_integer'] = 11;
 $event->send();

When I invoke the homeController test method in another browser, my client should be able to listen to the event and the console should print "IT's WORKING". However, that is not happening. My log is showing the following error:

warn: handshake error INVALID SERVER: server host 127.0.0.1 not allowed for /server
debug: websocket writing 7::/server:undefined

The server side codes seems to not work at all. Could I get some advice on how to get it done. I feel I am close and yet far.

The server.config.js setting is:

module.exports = {
    host : 'bigcat',
    port : parseInt('3001'),
    origin : 'bigcat:*',
    allowedServers : ["127.0.1.1"],
    dbOptions : {"driver":"dummy","config":[]},
    checkClientOrigin : 1,
    sessionVarName : 'PHPSESSID'
};

回答1:

I think you need to add 127.0.0.1 in yii config file -> node socket configuration section -> parameter is allowedServerAddresses

Like this^

'nodeSocket' => array(
    'class' => 'application.extensions.yii-node-socket.lib.php.NodeSocket',
    'host' => 'localhost',  // default is 127.0.0.1, can be ip or domain name, without http
    'port' => 3001      // default is 3001, should be integer,
    <b>'allowedServerAddresses' => array('127.0.0.1')</b>
)

And can you show node-socket/lib/js/server/server.config.js config file?