我遇到了最近更新到1.4.5时出现的问题。 (这已经工作了2年)。因此,当我向服务器发送client-msg
时,服务器会收到msg,然后应该将msg发送回所有连接的套接字(包括它自己)。我现在遇到的问题是我不能再向所有套接字发送消息。如果我将io.emit更改为socket.emit,我收到server-response
就好了,但当然,这只适用于那个套接字。我甚至去了(稀疏的)socket.io文档并尝试使用发射器库而没有任何运气。我错过了什么?
服务器端:
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 );
}
});
答案 0 :(得分:0)
运行调试并在线搜索类似错误后,我发现了这篇文章:https://github.com/socketio/socket.io/issues/2378
原来我没有运行socket.io的兼容版本,socket.io-redis在发布时是socket.io v1.4.4和socket.io-redis 1.0.0