连接到Sails WebSocket [英] Connect to Sails WebSocket

查看:135
本文介绍了连接到Sails WebSocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Sails框架V0.12开发一个简单的Hello World Websocket。我需要将外部基本前端(HTML,CSS,JS)连接到该Sails WebSocket服务器。发送一个简单的事件并检索'Hello World !!'我正在使用Sails API sails.io.js。



我在Front-End中试过这个

 <!doctype html> 
< html>

< head>
< script type =text / javascriptsrc =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 responses 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()>点击我< /按钮>
< / body>

< / html>

我知道这是连接到Sails,但我无法获得WebSocket响应。风帆响应是404(未找到)。我如何创建使用Sails套接字的WebSocket。

解决方案

您可以试试这个,
工作在风帆0.12上



views / user / *。ejs

 < div id ='messages'>< / DIV> 
< button id =pagar>点击我< /按钮>
< script>
$('#pagar')。click(function(){
io.socket.post('/ user / message',{message:'hi world'});

});

< / script>

api / controllers / UserController.js

  module.exports = {
message:function(req,res){

var io = sails.io;
//发送到所有套接字
io.sockets.emit('code',{message:req.param('message')});





前端js
assets / js / app.js

  io.socket.on('connect',function(){


io.socket.on('code',function(data){

$(#messages)。text(data.message);
console.log('Sails respond:',data.message);
}


I'm triying to develop a simple Hello World Websocket using Sails framework V0.12. I need to connect an external Basic Front-End (HTML,CSS,JS) to that Sails WebSocket server. Send a simple event and retrieving 'Hello World!!' I'm using Sails API sails.io.js.

I have tried this in Front-End

<!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>

I know that is connecting to Sails, but I I'm not able to get WebSocket response. Sails response is 404 (Not found). How can i create that WebSocket Using Sails Socket. Thanks a lot!

解决方案

you can try this, working on sails 0.12

views/user/*.ejs

<div id='messages'></div>
<button id="pagar">Click me</button>
<script>
$('#pagar').click(function(){     
io.socket.post('/user/message', {message: 'hi world'});

});

</script>

api/controllers/UserController.js

  module.exports = {
      message: function(req, res) { 

            var io = sails.io;
            // emit to all sockets            
            io.sockets.emit('code', {message: req.param('message')});

        }
    }

//Front-End js assets/js/app.js

 io.socket.on('connect', function() {


    io.socket.on('code', function (data) {

         $("#messages").text(data.message);
        console.log('Sails responded: ', data.message);
    }

这篇关于连接到Sails WebSocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆