问题描述
我正在尝试在客户端使用 Sails.js 和 Sails.io.js 创建一对一聊天.
I'm attempting to create a one to one chat using Sails.js and sails.io.js on the client side.
我可以让 io.socket.get
和 io.socket.post
工作,但我无法从 接收任何东西按照此处的说明,sails.sockets.broadcast
或 Model.publish
:
I can get the io.socket.get
and io.socket.post
to work, but I haven't been able to receive anything from either sails.sockets.broadcast
or Model.publish
as instructed here:
或这里
Sails.js + socket.io:发送从服务器到客户端的消息
服务器端代码:
UserController.js
UserController.js
module.exports = {
listen: function(req, res) {
console.log("about to join " + userId);
sails.sockets.join(req.socket, req.param('userId'));
}
};
来自:http://beta.sailsjs.org/#/documentation/reference/sails.sockets/sails.sockets.join.html
MessageController.js
MessageController.js
// Some code to get userId and message model here
console.log("about to broadcast on " + userId);
sails.sockets.broadcast(userId, 'conversation_message', message);
// Did not JSONify the message model, not sure if I need to?
来自:http://beta.sailsjs.org/#/documentation/reference/sails.sockets/sails.sockets.broadcast.html
客户端代码:
// Some code to get userId...
io.socket.get('/user/listen', {
userId: userId
}, function() {
io.socket.on('conversation_message', function(message) {
console.log("we have a message");
});
});
当我使用userId
点击触发广播的路由时,没有任何内容发送到此客户端代码以记录我们有一条消息
.有什么想法吗?
When I hit the route that triggers the broadcast with userId
, nothing is sent to this client code to log we have a message
. Any ideas?
我也尝试过 Model.subscribe/Model.publish 方法,但没有成功——同样的问题.
更新:listen
函数实际上并没有返回——正如有人在他们后来删除的评论中建议的那样.将 res.send(200);
添加到 listen
函数的末尾就足以让它工作了.
Update: The listen
function doesn't actually return -- as someone suggested in a comment that they later deleted. Adding res.send(200);
to the end of the listen
function was enough to make it work.
如果那个人再次添加他们的评论作为答案,我会接受.
If that person would add their comment again as an answer I'll accept it.
推荐答案
(那是我.我认为我的评论可能不值得,因为我重读了你的帖子,你说你有工作,所以我认为我错了.我的评论是下面的大意,但我不记得逐字)
(That was me. I thought my comment might of been not worthwhile because i reread your post and you said you had the get working so I thought I was wrong. My comment was something to the effect below, but I don't remember verbatim)
您是否检查过以确保您确实在客户端收听中收到了响应?您实际上在整个代码中获得了哪些日志语句?将 console.log('get user listen') 或其他内容放在客户端 socket.get 中.
Have you checked to make sure the you are actually getting a response on the client-side listen? What log statements are you actually getting throughout your code? Put a console.log('get user listen') or something inside of the Client-side socket.get.
这篇关于使用sails.sockets.join和sails.sockets.broadcast向客户端发送Sails.js不会向客户端返回信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!