【发布时间】:2016-09-04 21:56:54
【问题描述】:
我想在我基于sailjs 的项目中创建一个聊天应用程序,我如何以及在哪里可以配置服务器中的套接字和相关设置?我从这个存储库获得了一个示例项目,但它使用的是sails v0.10,我需要使用sails v0.11.3,但是v0.11.3在Nodejs脚本中使用socket时有很多变化,例如,我们不能使用onConnect 因为它已被弃用。
我已经尝试过这个例子,但没有在sail v0.11.3 中使用 https://github.com/sgress454/sailsChat
这是我已经完成的代码,但我不知道我应该把它放在哪里,当我把它放在 config 目录中的 sockets.js 文件中时它不起作用
// Set some options:
// (you have to specify the host and port of the Sails backend when using this library from Node.js)
io.sails.url = 'http://localhost:9002';
// ...
io.socket.on('connect', function socketConnected() {
console.log('connect..');
});
// Send a GET request to `http://localhost:1337/hello`:
io.socket.get('/hello', function serverResponded (body, JWR) {
// body === JWR.body
console.log('Sails responded with: ', body);
console.log('with headers: ', JWR.headers);
console.log('and with status code: ', JWR.statusCode);
// When you are finished with `io.socket`, or any other sockets you connect manually,
// you should make sure and disconnect them, e.g.:
io.socket.disconnect();
// (note that there is no callback argument to the `.disconnect` method)
});
请指导我。
【问题讨论】:
-
我没用过sails.io.js,但是你能不能像普通的socket.io一样试试
io.socket.on('connection'..而不是'connect'?"Sails responded with"消息会出现吗? -
目前我把它放在我的 app.js 文件中,但“连接”不起作用。主要问题是,我不知道我应该把这段代码放在哪里
-
它应该在你引用
sails.io.js之后的任何地方,像这样:sailsjs.org/documentation/reference/web-sockets/socket-client/… ? -
好的,让我检查一下。谢谢:)
标签: node.js sockets sails.js sails.io.js