【发布时间】:2015-10-02 08:31:48
【问题描述】:
我正在尝试与 nodeJs 进行实时聊天。但我遇到了一些问题。
部分的console.logio.on('connection', function(socket)
{
console.log("A user connected");
});
根本没有 console.log。
当我遇到困难时,我正在寻找指南。我构建的几乎与指南中的相同。
这是我的html页面:
<!doctype html>
<html>
<head>
<title>
Socket.IO chat
</title>
<link rel="stylesheet" href="./CSS/style.css" media="screen" title="no title" charset="utf-8">
</head>
<body>
<ul id="messages">
</ul>
<form action="">
<input id="m" autocomplete="off" />
<button>
Send
</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
</body>
</html>
这里是我的 js 文件:
var app = require("express")(),
http = require("http").Server(app),
io = require("socket.io")(http),
port = 3000;
app.get('/', function(req, res)
{
res.sendFile(__dirname + "./index.html");
});
io.on('connection', function(socket)
{
console.log("A user connected");
});
http.listen(port, function()
{
console.log('Listening on port: ' + port);
});
我的文件结构:
^因为我认为它与页面之间的链接有关。
我正在使用本指南:http://socket.io/get-started/chat/
提前致谢, 我希望得到一些答案,所以我可以继续这个:) 干杯
【问题讨论】:
-
在cmd中进入index.js的文件夹,然后运行“node index.js”。这样做之后,你在 cmd 中看到“listening on port: 3000”还是看到错误?
-
是的,我可以在我的控制台中看到“正在侦听端口 3000”.. 完全没有错误
-
在浏览器中访问 localhost:3000 后,浏览器控制台中是否显示任何内容?
-
应该是socket.io没有正确安装。当你运行“npm install -- save socket.io”时,你确定你在正确的文件夹中吗?在包含 index.js 的文件夹中再次运行它以确保。
标签: javascript node.js express socket.io chat