无法从服务器接收数据到客户端(node.js jquery)

时间:2017-05-09 14:20:28

标签: jquery node.js sockets web-applications

这是我的 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);
});

这是我的

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

现在问题是客户端没有收到任何数据数据 我删除

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

我的按钮有效,但仍然无法从客户端提供。你能帮帮我们吗。

0 个答案:

没有答案