【发布时间】:2017-06-21 23:15:06
【问题描述】:
我正在尝试使用 Sails 框架 V0.12 开发一个简单的 Hello World Websocket。我需要将外部基本前端(HTML、CSS、JS)连接到 Sails WebSocket 服务器。发送一个简单的事件并检索“Hello World!!”我正在使用 Sails APIsails.io.js。
我已经在前端尝试过这个
<!doctype html>
<html>
<head>
<script type="text/javascript" src="js/dependencies/sails.io.js"></script>
<script type="text/javascript">
window.onload = function() {
io.sails.url = 'http://localhost:1337';
io.socket.get('http://localhost:1337/helloworld', function(body, sailsResponseObject) {
console.log('Sails responded with: ', body);
console.log('with headers: ', sailsResponseObject.headers);
console.log('and with status code: ', sailsResponseObject.statusCode);
});
};
</script>
</head>
<body>
<ul id='messages'></ul>
<button onclick="pagar()">Click me</button>
</body>
</html>
我知道它正在连接到 Sails,但我无法获得 WebSocket 响应。 Sails 响应为 404(未找到)。如何使用 Sails 套接字创建该 WebSocket。非常感谢!
【问题讨论】:
-
尝试从 io.socket.get 中删除 localhost:1337... 我认为 Sails 套接字客户端将第一个参数附加到 io.sails.url。
-
希望this能帮到你
-
确保在 Sails 中 config/routes.js 包含到 helloworld 的路由或在 config/blueprints.js 中启用了休息
-
留作参考,网址为http://[your_ip]:1337,以便网络上多台设备共享前端
标签: javascript html node.js websocket sails.js