![](/img/trans.png)
[英]Socket.io browser client not receiving emit events from socket.io client
[英]socket.io client not receiving io.emit from server ( using redis adapter )
我有一個問題,最近更新到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 );
}
});
運行調試並在線搜索類似錯誤后,我發現了以下信息: https : //github.com/socketio/socket.io/issues/2378
原來我沒有運行兼容版本的socket.io,socket.io-redis,在發布時它們是socket.io v1.4.4和socket.io-redis 1.0.0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.