res.sendFile不是一个函数。
以下是我的服务器js代码
var app = require('express')();
var http = require('http').Server(app);
var fs = require('fs');
var io = require("socket.io")(http);
var users = {};
app.get('/', function(req, res) {
res.sendFile(__dirname + '/client.html');
});
app.listen(3000)
io.on("connection", function (client) {
client.on("join", function (name) {
users[client.id] = name;
client.emit("update", "Connected to Server");
socket.sockets.emit("update", name + " has entered the confession room");
socket.sockets.emit("update-users", users);
});
client.on("send", function (msg) {
socket.sockets.emit("chat", users[client.id], msg);
});
client.on("disconnect", function () {
socket.sockets.emit("update", users[client.id] + " has left the confession room");
delete users[client.id];
socket.sockets.emit("update-users", users);
});
});
我尝试过的事情: - 检查参数的顺序。 - 安装快速模块。
答案 0 :(得分:1)
你有这两行代码的重复服务器,你将它传递给你永远不会启动的socket.io:
var http = require('http').Server(app); // one on port 80
app.listen(3000); // one on port 3000
删除第一个,然后修改您的代码,如下所示。并且,确保您在Express服务器和socket.io服务器所在的端口3000上进行连接。
或者,如果您希望服务器和socket.io位于端口80上,则更改为:
const server = app.listen(80);
所以,你应该最终得到这个:
const app = require('express')();
const fs = require('fs');
const users = {};
app.get('/', function(req, res) {
res.sendFile(__dirname + '/client.html');
});
const server = app.listen(3000);
const io = require("socket.io")(server);
// various socket.io code here