Node.js soocket使用nodejs react应用程序多次发射

Node.js soocket使用nodejs react应用程序多次发射,node.js,reactjs,socket.io,react-redux,Node.js,Reactjs,Socket.io,React Redux,我正在使用socket.io制作讨论论坛实时应用程序,但它的dispatch redux操作3次,然后6次,很快,但当我连接控制台用户时,它显示一个用户只连接了一次,函数emit函数也运行了一次,但socket.on运行时与redux开发工具中的上述模式相同,添加注释操作运行了3次然后6次 let socket = io('http://localhost:5000', { transports: [ 'websocket' ], upgrade: false, reco

我正在使用socket.io制作讨论论坛实时应用程序,但它的dispatch redux操作3次,然后6次,很快,但当我连接控制台用户时,它显示一个用户只连接了一次,函数emit函数也运行了一次,但socket.on运行时与redux开发工具中的上述模式相同,添加注释操作运行了3次然后6次

let socket = io('http://localhost:5000', {
    transports: [ 'websocket' ],
    upgrade: false,
    reconnection: false
});
const DiscussionForum = (props) => {
    const dispatch = useDispatch();
    useEffect(() => {
        dispatch(allPost());
        socket.emit('Discussionforum', 'connected...');

        return () => {
            console.log('out');
            socket.disconnect();
        };
    }, []);
    socket.on('allcomment', (allcomment) => {
        console.log('running 3 timess');
        props.addComment(allcomment);
    });

    socket.on('addupvote', (addupvote) => {
        props.addupvote(addupvote);
    });
    socket.on('commentvote', (commentvote) => {
        props.addComment(commentvote);
    });
    socket.on('verifycomment', (verifycomment) => {
        props.addComment(verifycomment);
    });
    socket.on('deupvote', (deupvote) => {
        props.addupvote(deupvote);
    });
    socket.on('addpoll', (addpoll) => {
        props.postpoll(addpoll);
    });
    socket.on('deletecomment', (deletecomment) => {
        props.deleteComment(deletecomment);
    });
    
    const posts = props.postsloading ? (
        <h3>Loading</h3>
    ) : (
        <div className="posts-container">
    
            {props.allPost.map((post, index) => {
                return (
                    <Post
                        socket={socket}
                        queryimage={post.queryimage}
                        post={props.allPost}
                        poll={post.poll}
                        usernameinfo={props.usernameinfo}
                        avatarinfo={props.avatarinfo}
                        key={index}
                        index={index}
                        postId={post._id}
                        userId={post.id}
                        postDetail={post.query}
                        comments={post.comment}
                        subject={post.subject}
                        user={post.username}
                        upvote={post.upvote}
                        avatar={post.avatar}
                        tags={post.tags}
                        postdate={post.postdate}
                        question={post.question}
                    />
                );
            })}
        </div>
    );

    return (
        <div className="discussion-forum-view">
    
                <AddPost
                    modalClosed={notSubmitting}
                    userCourse={props.course}
                    useridinfo={props.useridinfo}
                    usernameinfo={props.usernameinfo}
                    avatarinfo={props.avatarinfo}
                />
            </Modal>
        </div>
    );
};
const mapStateToProps = (state) => {
    return {
        allPost: state.user.allpost
    };
};
const mapDispatchToProps = (dispatch) => {
    return {
        addComment: function(postid) {
            dispatch(addComment(postid));
        },
        addupvote: function(postid) {
            dispatch(addupvote(postid));
        },
        deletePost: function(postid) {
            dispatch(deletePost(postid));
        },
        deleteComment: function(postid) {
            dispatch(deleteComment(postid));
        },
        postpoll: function(postid) {
            dispatch(postpoll(postid));
        }
    };
};
export default connect(mapStateToProps, mapDispatchToProps)(DiscussionForum);
let socket=io('http://localhost:5000', {
传输:['websocket'],
升级:false,
重新连接:错误
});
const DiscussionForum=(道具)=>{
const dispatch=usedpatch();
useffect(()=>{
调度(allPost());
emit('Discussionforum','connected…');
return()=>{
console.log('out');
socket.disconnect();
};
}, []);
socket.on('allcomment',(allcomment)=>{
console.log(“运行3次”);
props.addComment(allcomment);
});
socket.on('addupvote',(addupvote)=>{
道具addupvote(addupvote);
});
socket.on('commentvote',(commentvote)=>{
props.addComment(commentvote);
});
socket.on('verifycomment',(verifycomment)=>{
道具添加注释(验证注释);
});
socket.on('deupvote',(deupvote)=>{
道具添加投票(deupvote);
});
socket.on('addpoll',(addpoll)=>{
后期投票(addpoll);
});
socket.on('deletecomment',(deletecomment)=>{
props.deleteComment(deleteComment);
});
const posts=props.postsloading(
加载
) : (
{props.allPost.map((post,index)=>{
返回(
);
})}
);
返回(
);
};
常量mapStateToProps=(状态)=>{
返回{
allPost:state.user.allPost
};
};
const mapDispatchToProps=(调度)=>{
返回{
addComment:函数(postid){
调度(添加评论(postid));
},
addupvote:函数(posted){
发送(添加投票(postid));
},
deletePost:函数(postid){
派遣(删除邮寄(邮寄));
},
deleteComment:函数(postid){
派遣(删除评论(张贴));
},
后付费:功能(postid){
派递(邮递);;
}
};
};
导出默认连接(mapStateToProps、mapDispatchToProps)(讨论论坛);
在post组件中,我正在传递socketio,代码是

     const Post = (props) => {

    const handleKeyDown = (e) => {
        if (e.key === 'Enter') {
            props.socket.emit('addcomment', {
                postId: props.postId,
                avatar: props.avatarinfo,
                username: props.usernameinfo,
                comment: comment
            });
            setComment('');
            setShowComments(true);
        }
    };


    return (
        <div className="post">
    
            <div className="post-content">
                <div className="post-details">
            
                    <div className="post-user-details">
                        <span className="post-user">{props.user}</span>
                        <span className="post-timing">{moment(props.postdate).fromNow()}</span>
                    </div>

                    {props.user === props.usernameinfo ? (
                        <img
                            src={more}
                            onClick={() => {
                                props.deletePost(props.postId);
                            }}
                        />
                    ) : null}
                </div>
                <p className="post-subject">{props.subject}</p>


                <div className="post-query">
        
                    {props.queryimage === 'NoImage' ? null : (
                        <img
                            className="query-image"
                            src={'http://localhost:5000/static/uploads/' + props.queryimage}
                            alt="product"
                        />
                    )}
                    
                            
            </div>
            <div className="post-button-div">
                <button
                    className="post-button"
                    onClick={() => {
                        props.socket.emit('deupvote', {
                            postId: props.postId,
                            username: props.usernameinfo
                        });
                    }}
                >
                    <i className="far fa-thumbs-up" /> Devotes : {props.upvote.length}
                </button>
                <button
                    style={props.upvote.includes(props.usernameinfo) ? { backgroundColor: 'black' } : null}
                    className="post-button"
                    onClick={() => {
                        // props.socket.emit('Discussionforum', 'hello');
                        props.socket.emit('addupvote', {
                            postId: props.postId,
                            username: props.usernameinfo
                        });
            
                    }}
                >
                    <i className="far fa-thumbs-up" /> Upvotes : {props.upvote.length}
                </button>
                <button className="post-button" onClick={showCommentSection}>
                    <i className="far fa-comment-alt fa-sm" /> Comments : {props.comments.length}
                </button>
            </div>

            <div className="comment-section">
                {props.post[props.index].comment.map((comment, index) => (
                    <div className="comment-div" key={index} style={showComments ? null : { display: 'none' }}>
                        <img
                            className="comment-avatar"
                            src={'http: //localhost:5000/static/Avatars/avatar' + comment.avatar + '.svg'}
                            alt="avatar"
                        />
                        {/* <h1>{comment.avatar}</h1> */}
                        <div className="comment-details">
                            <div className="comment-info">
                                <span className="comment-username">{comment.username}</span>
                                <span className="comment-date">{moment(comment.commentdate).fromNow()}</span>
                            </div>
                            {comment.username === props.usernameinfo ? (
                                <button
                                    className="post-button"
                                    onClick={() => {
                                        props.socket.emit('deletecomment', {
                                            postId: props.postId,
                                            commentid: comment._id
                                        });
                                    }}
                                >
                                    <i className="fas fa-trash" />
                                </button>
                            ) : null}
                            {props.user === props.usernameinfo ? (
                                <button
                                    style={comment.verify ? { backgroundColor: 'green' } : null}
                                    className="post-button"
                                    onClick={() => {
                                        props.socket.emit('verifycomment', {
                                            postId: props.postId,
                                            commentid: comment._id,
                                            verify: 11
                                        });
                                    }}
                                >
                                    verify
                                </button>
                            ) : null}
                            <p className="comment">{comment.userComment}</p>
                            <button
                                className="post-button"
                                onClick={() => {
                                    props.socket.emit('commentvote', {
                                        postId: props.postId,
                                        commentid: comment._id,
                                        username: props.usernameinfo
                                    });
                                }}
                            >
                                vote{comment.commentvote.length}
                            </button>
                        </div>
                    </div>
                ))}
                <input
                    className="comment-input"
                    type="text"
                    name="comment"
                    onChange={onChangeHandler}
                    onKeyDown={handleKeyDown}
                    value={comment}
                    placeholder="Enter a comment ...."
                />
            </div>
        </div>
    );
};
const mapStateToProps = (state) => {
    return {
        postprofile: state.user.postprofile,
        allPost: state.user.allpost,
        postprofileloading: state.user.postprofileloading
    };
};
const mapDispatchToProps = (dispatch) => {
    return {
        showprofile: function(userid) {
            dispatch(showprofile(userid));
        }
    };
};

export default connect(mapStateToProps, mapDispatchToProps)(Post);
const Post=(道具)=>{
常量handleKeyDown=(e)=>{
如果(e.key=='Enter'){
props.socket.emit('addcomment'{
posted:props.posted,
阿凡达:道具,
用户名:props.usernameinfo,
评论:评论
});
setComment(“”);
setShowComments(true);
}
};
返回(
{props.user}
{moment(props.postdate).fromNow()}
{props.user===props.usernameinfo(
{
props.deletePost(props.posted);
}}
/>
):null}

{props.subject}

{props.queryimage==='NoImage'?空:( )} { props.socket.emit('deupvote'{ posted:props.posted, 用户名:props.usernameinfo }); }} > 致力于:{props.upvote.length} { //props.socket.emit('Discussionforum','hello'); props.socket.emit('addupvote'{ posted:props.posted, 用户名:props.usernameinfo }); }} > upvote:{props.upvote.length} 注释:{props.Comments.length} {props.post[props.index].comment.map((comment,index)=>( {/*{comment.avatar}*/} {comment.username} {时刻(comment.commentdate).fromNow()} {comment.username==props.usernameinfo( { props.socket.emit('deletecomment'{ posted:props.posted, commentid:注释。\u id }); }} > ):null} {props.user===props.usernameinfo( { props.socket.emit('verifycomment'{ posted:props.posted, commentid:comment.\u id, 核实:11 }); }} >