Tuesday, 9 May 2017

Cannot receive data from server to client (node.js jquery)

This is my server.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;

app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});

http.listen(port, function(){
console.log('listening on *:' + port);
});

And this is my

index.html

<button>Click on this paragraph.</button>
<p id = "try">Click me!</p>
<!-- <form action="">
  <input id="m" autocomplete="off" />
</form> -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.3/socket.io.js">
</script>
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<script>

    $(document).ready(function(){

        var socket = io();

        $("button").click(function(){
            socket.emit('chat message',$('#try').text("You clicked the button"));
                setInterval(function(){
                    $('#try').text('Click me!');
                }, 3000);
                return false;
 });
 socket on('chat message', function(msg){
            $('#try').text(msg);

        });
    });
 </script>

Now the problem here is that the client doesnt receive any data data and i remove

 socket on('chat message', function(msg){
            $('#try').text(msg);    
        });

my button works but still doesn't feed from the client side . Can you help me guys please.



via TheGinxx009

No comments:

Post a Comment