zexeo_tutorials icon indicating copy to clipboard operation
zexeo_tutorials copied to clipboard

redux-chat项目 重复发送消息的问题

Open lcoder opened this issue 7 years ago • 5 comments

聊天项目中,服务启动的时候。当发送newMessage的时候 在redux-chat/src/server/io.js中

socket.rooms.forEach( ( room , index )=>{
        console.log( 'should leave room , skip first one' ) ;
        if( index > 0 ){
            socket.leave( room ) ;
        }
    } ) ;

控制台报错了

/zexeo_tutorials/redux-chat/src/server/io.js:50
	socket.rooms.forEach(function (room, index) {
	             ^
TypeError: socket.rooms.forEach is not a function
    at switchRoom (/Users/maotingfeng/demo/zexeo_tutorials/redux-chat/src/server/io.js:45:15)
    at Socket.<anonymous> (/Users/maotingfeng/demo/zexeo_tutorials/redux-chat/src/server/io.js:15:13)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at /Users/maotingfeng/demo/zexeo_tutorials/redux-chat/node_modules/socket.io/lib/socket.js:503:12
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickDomainCallback (internal/process/next_tick.js:122:9)

返回的socket.rooms类型为Object了,不是Array类型了,不知道怎么处理?

lcoder avatar Feb 25 '17 14:02 lcoder

git clone 这个项目的时候,什么都没改动,直接跑,也是这个问题。

lcoder avatar Feb 25 '17 14:02 lcoder

试试 for(let ii =0; ii < socket.rooms.length; ii++) { socket.rooms[ii] }

eisneim avatar Feb 27 '17 14:02 eisneim

我用了Object.keys遍历了rooms,报错是不报错了,但是每次消息都发送了两条。

Object.keys( socket.rooms ).forEach( ( room , index )=>{
        console.log( 'should leave room , skip first one' ) ;
        if( index > 0 ){
            socket.leave( room ) ;
        }
    } ) ;

你的意思是这种改法嘛?

lcoder avatar Feb 27 '17 14:02 lcoder

@lcoder 你去看看socketio的官方文档吧,版本可能不同,我也不怎么写js了

eisneim avatar Feb 28 '17 04:02 eisneim

@eisneim 好的,谢谢。

lcoder avatar Feb 28 '17 05:02 lcoder