我的代码正确但在节点js socket.io
上仍然是错误的。我该如何解决?
app.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req,res){
res.sendfile('index.html');
});
var clients = 0;
io.on('connection', function(socket){
clients++;
io.sockets.emit('boardcast', {message: clients + ' clients connected!'});
socket.on('disconnect', function(){
clients--;
io.sockets.emit('boardcast', {message: clients + ' clients connected!'});
});
});
http.listen(3000, function(){
console.log('start server on port :3000');
});
的index.html
<html>
<head></head>
<body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>
<script>
var socket = io();
socket.on('broadcast', function(data){
document.body.innerHTML = '';
document.write(data.message);
});
</script>
<h1>hello world</h1>
</body>
</html>
SSH
node app.js
返回值
start server on port :3000
express deprecated res.sendfile: Use res.sendFile instead app.js:6:6