socket.io client not receiving io.emit from server ( using redis adapter )

百般思念 提交于 2019-12-12 03:18:57

问题


I have a problem which started when updated to 1.4.5 recently. ( This has been working for the last 2 years ) . So when I send a client-msg to the server, the server receives the msg and then is supposed to emit the msg back to all the connected sockets ( including itself ). The problem I am now having is I can no longer emit messages to all the sockets. If I change the io.emit to socket.emit I receive the server-response just fine, but of course, that's only for that one socket. I even went to the (sparse) socket.io docs and tried to use the emitter library without any luck. What am I missing?

Server side:

var redisPort = 6379
var io = require('socket.io');
var redisAdapter = require('socket.io-redis');
var emitter = require('socket.io-emitter')( { host: 'localhost' , port : redisPort } );//6379

io = io.listen( httpServer ); //express server
io.adapter( redisAdapter({ host: 'localhost' , port : redisPort }) );//6379
io.on('connection', function( socket ){

    socket.on('client-msg', function( data ){
        var msgObject = { content : '' , status : 0 };
        if( data && data.content ){
            msgObject.content = data.content;
            msgObject.status = 1;
        }

        io.emit('server-response', msgObject );//doesn't work ( noone gets the data )
        emitter.emit('server-response', msgObject ); //doesn't work ( noone gets the data )
        socket.emit( 'server-response', msgObject );//works ( just the one socket gets the data )

    });

    socket.on('disconnect', function( data ) { 
        //do some stuff unrelated to emitting                           
    });

});

Client side:

var socketio = io.connect( window.location.origin  , { transports : ['websocket'] } );

socketio.emit("client-msg" , { content : msg  });

socketio.on("server-response", function( data ) {

    if( data.status == 1 ){
        displayTheMessage( data );
    }

});

回答1:


After running the debug and searching for similar errors online I found this post: https://github.com/socketio/socket.io/issues/2378

Turns out I was not running compatible version of socket.io, socket.io-redis which at the time of this posting are socket.io v1.4.4 and socket.io-redis 1.0.0



来源:https://stackoverflow.com/questions/38798550/socket-io-client-not-receiving-io-emit-from-server-using-redis-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!