简体   繁体   English

socket.io客户端未从服务器接收io.emit(使用Redis适配器)

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

I have a problem which started when updated to 1.4.5 recently. 我有一个问题,最近更新到1.4.5时就开始了。 ( This has been working for the last 2 years ) . (这已经工作了近2年)。 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 ). 因此,当我将client-msg发送到服务器时,服务器会收到msg,然后应该将msg发送回所有连接的套接字(包括本身)。 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. 如果将io.emit更改为socket.emit,我会收到server-response ,但当然,那仅适用于那个套接字。 I even went to the (sparse) socket.io docs and tried to use the emitter library without any luck. 我什至去了(稀疏的)socket.io文档,并尝试使用发射器库而没有任何运气。 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 );
    }

});

After running the debug and searching for similar errors online I found this post: https://github.com/socketio/socket.io/issues/2378 运行调试并在线搜索类似错误后,我发现了以下信息: 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 原来我没有运行兼容版本的socket.io,socket.io-redis,在发布时它们是socket.io v1.4.4和socket.io-redis 1.0.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM