socket.io
에 문제가 있습니다. 내 코드 router.post(/comment,...)
에서 사용자 의견을 데이터베이스에 저장하고 (몽구스 사용) emit
이 저장됩니다. 컨트롤러 기능에서 readMoreCourse
은 데이터베이스의 모든 주석을 가져와 표시하는 것입니다 (그리고 ng-reapat를 사용하여 실시간으로 주석을 표시하는이 함수에 소켓을 사용하는 방법에 대해 질문하십시오). 기능 AddComment
은 클라이언트 쪽 chceck 유효한 양식과 데이터베이스에 대한 다음 게시물 주석에 있습니다. 내 질문 : 실시간 저장 및 표시 각도 (ng-repeat?) 및 socket.io 사용하여 사용자 의견을 표시하는 방법? 솔직히 나는 이것을 처음 만들었고 어떤 도움을 주셔서 감사합니다.angularjs 및 socket.io를 사용하여 실시간으로 주석을 저장하고 표시합니다.
서버
io.on('connection', function(socket){
socket.emit('comment', function(){
console.log('Comment emitted')
})
socket.on('disconnect', function(){
})
})
API
router.post('/comment', function(req, res) {
Product.findOne({ _id: req.body._id }, function(err, product){
if(err) {
res.json({ success:false, message: 'Course not found' })
} else {
User.findOne({ username: req.decoded.username }, function(err, user){
if(err){
res.json({ success:false, message: 'Error'})
} else {
product.comments.push({
body: req.body.comment,
author: user.username,
date: new Date(),
});
product.save(function(err){
if(err) throw err
res.json({ success: true, message: 'Comment added })
**io.emit('comment', msg);**
})
}
})
}
})
})
컨트롤러
Socket.connect();
User.readMoreCourse($routeParams.id).then(function(data){
if(data.data.success){
app.comments = data.data.product.comments;
} else {
$window.location.assign('/404');
}
});
app.AddComment = function(comment, valid) {
if(valid){
var userComment = {};
userComment.comment = app.comment;
Socket.on('comment', User.postComment(userComment).then(function(data){
if(data.data.success){
$timeout(function(){
$scope.seeMore.comment = '';
},2000)
} else {
app.errorMsg = data.data.message;
}
}));
} else {
app.errorMsg = 'Error';
}
}
$scope.$on('$locationChangeStart', function(event){
Socket.disconnect(true);
})
공장
userFactory.readMoreCourse = function(id) {
return $http.get('/api/seeMore/' + id)
}
userFactory.postComment = function(comment){
return $http.post('/api/comment', comment);
}
.factory('Socket', function(socketFactory){
return socketFactory()
})
컨트롤러 코드 'Socket.on'이 작동합니까? –
기본적으로 소켓 부분을 제거하면이 코드가 제대로 작동하지만 모든 것이 데이터베이스에 저장되는 것이 적절하지만 지금은 작동하지 않습니다. 컨트롤러가 작동하지 않지만 공장에서 작동 중입니다. 콘솔, 로그에 메시지 표시 –