gpt4 book ai didi

javascript - Socket.io 教程不打印到控制台的连接

转载 作者:行者123 更新时间:2023-11-30 16:56:29 25 4
gpt4 key购买 nike

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>

最佳答案

问题出在你的html文件的这部分:

<script src="socket.io/socket.io.js">
var socket = io();
</script>

你需要像这样先加载socket.io库:

<script src="/socket.io/socket.io.js"></script>

然后实例化socket.io对象:

<script>
var socket = io();
</script>

关于javascript - Socket.io 教程不打印到控制台的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29635235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com