var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + "/index.html");
});
http.listen(3000, function() {
console.log('listening on 3000');
});
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
我不明白为什么它不会打印"用户连接"当我刷新页面时,我想知道为什么会这样。以下是index.html文件。我正在按照入门教程(http://socket.io/get-started/chat/)
进行操作<!DOCTYPE html>
<html>
<head>
<title>Socket.IO chat</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font: 13px Helvetica, Arial;
}
form {
background: #000;
padding: 3px;
position: fixed;
bottom: 0;
width: 100%;
}
form input {
border: 0;
padding: 10px;
width: 90%;
margin-right: 0.5%;
}
form button {
width: 9%;
background: rgb(130, 224, 255);
border: none;
padding: 10px;
}
#messages {
list-style-type: none;
margin: 0;
padding: 0;
}
#messages li {
padding: 5px 10px;
}
#messages li:nth-child(odd) {
background: #eee;
}
</style>
</head>
<body>
<ul id="messages">
<form action="">
<input id="m" autocomplete="off" />
<button>Send</button>
</form>
</ul>
<script src="socket.io/socket.io.js">
var socket = io();
</script>
</body>
答案 0 :(得分:2)
<script src="socket.io/socket.io.js">
var socket = io();
</script>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>