[英]Sails.js websockets work in localhost but not working after I deploy to Openshift
摘要:
我正在为基于sails.js的网站的管理面板工作。 我已经配置了websockets以在我的localhost中工作。 但是当我部署到openshift时,只有io.socket.on('connect',...); 正在开火。 但是当我发布添加记录时,客户端没有收到任何内容。
细节:
首先是关于我的配置的一些细节:
目前的行为:
我正在尝试为我的应用程序启用通知系统,所以我使用默认的sails.io.js来连接我的客户端代码中的websocket(使用默认的io.socket对象)我现在使用的是console.log()记录任何事件。 代码在客户端js文件中如下所示:
$(document).ready(function () {
io.sails.environment = "development";
if(window.location.hostname === "localhost") {
io.sails.url = 'http://localhost:1337';
} else {
io.sails.url = 'http://myapp-username.rhcloud.com:8000';
}
var adminSocket = io.socket;
// Access socket's connect event to initialize functions
adminSocket.on('connect', function () {
console.log("Connected to socket");
// Subscribe to admin model of current user
adminSocket.get('/admin/subscribeToModel');
});
// Access notification (adminAlerts) model's socket events
adminSocket.on('admin', function(event) {
console.log(event);
});
// Log Errors
adminSocket.on('error', function (event) {
console.log(event);
});
});
正如您所看到的,代码在套接字连接后运行io.socket.get(...)。 这适用于localhost和openshift,并记录“已连接到套接字”以及sails.io.js的默认连接消息。 当服务器关闭时,客户端尝试重新连接作为默认行为。
io.socket.get在管理控制器中运行自定义控制器功能:
subscribeToModel: function(req, res, next) {
if (!req.isSocket) {
console.log("Not Socket");
return res.badRequest();
}
console.log("Socket");
Admin.subscribe(req, [req.session.userid]); //Subscribes to Admin model's entry with the user's id
return res.json({message: "Subscribed"});
}
这会在openshift和localhost中记录“Socket”,但返回消息不可用。 我不知道订阅是否在openshift中有效,但它确实在localhost中有效。
这是设置。 现在我构建了一个测试套接字行为的函数。 在管理员控制器中我创建:
createAndBroadcastNotification: function (req, res, next) {
AdminAlerts.create({
admin: req.session.admin.id,
alertHeader: 'Some Header',
alertBody: 'Some Message',
alertType: 'label-info',
url: '/somepage'
}).exec(function (err, alert) {
if (err) {
res.serverError(err);
}
Admin.findOne({ where: {id: req.session.admin.id} }).populate('alerts').exec(function (err, admin) {
if (err) {
res.serverError(err);
}
Admin.publishAdd(req.session.userid, 'alerts', alert.id);
return res.json({message: "Done"});
});
});
}
在此之后, openshift和localhost都在浏览器中显示{message:Done}并且正在创建记录并在mySQL数据库中关联。 但只有Localhost按预期在控制台中发布此消息:
Object {id: 1, verb: "addedTo", attribute: "alerts", addedId: 10}
Openshift没有显示此类消息。 它也没有显示任何错误消息。
我一直试图找出问题5天。 我无法找到为何发生这种情况的线索。
目的:
我希望在客户端中获取console.log()消息以表示正在添加的警报。
我想到了。 对于任何面临相同或类似问题的人来说,openshift的文档(在博客中)已经过时了。 让sails.io.js弄清楚主机和端口的工作“很好”。 它虽然在Firefox中返回连接错误。 以下是更改:
// client-side-code.js
$(document).ready(function () {
// io.sails.environment = "development";
// if(window.location.hostname === "localhost") {
// io.sails.url = 'http://localhost:1337';
// } else {
// io.sails.url = 'http://myapp-username.rhcloud.com:8000';
// }
var adminSocket = io.socket;
// Access socket's connect event to initialize functions
adminSocket.on('connect', function () {
console.log("Connected to socket");
// Subscribe to admin model of current user
adminSocket.get('/admin/subscribeToModel');
});
// Access notification (adminAlerts) model's socket events
adminSocket.on('admin', function(event) {
console.log(event);
});
// Log Errors
adminSocket.on('error', function (event) {
console.log(event);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.