socket.io客户端未从服务器接收io.emit(使用Redis适配器) [英] socket.io client not receiving io.emit from server ( using redis adapter )

查看:372
本文介绍了socket.io客户端未从服务器接收io.emit(使用Redis适配器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,该问题最近更新为1.4.5时开始. (这已经工作了两年了).因此,当我向服务器发送client-msg时,服务器接收到味精,然后应该将味精发送回所有连接的套接字(包括自身).我现在遇到的问题是我无法再向所有套接字发出消息.如果我将io.emit更改为socket.emit,我会收到server-response,但当然,这仅适用于那个套接字.我什至去了(稀疏的)socket.io文档,并尝试使用发射器库而没有任何运气.我想念什么?

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?

服务器端:

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                           
    });

});

客户端:

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

原来我没有运行兼容版本的socket.io,socket.io-redis,在发布时它们是socket.io v1.4.4和socket.io-redis 1.0.0

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客户端未从服务器接收io.emit(使用Redis适配器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆