Connect To Sails Websocket
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
Solution 1:
you can try this, working on sails 0.12
views/user/*.ejs
<divid='messages'></div><buttonid="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);
}
Post a Comment for "Connect To Sails Websocket"