/ / app.js
var app = require("express")();
// websocket
// ExpressAPPHTTP
var http = require("http").Server(app);
// socket.io
var io=require("socket.io")(http);
var users=[]
// connection
// connection
io.on("connection",function(socket){
socket.on("login",(data)=>{
console.log(data)
var userObj=Object.assign(data,{type:"online",sign:socket.id})
users.push(userObj)
io.emit("login",{data:{
users,
online:userObj
}
})
})
})
//
app.get("/", function(req, res){
res.sendFile("E:/vuechatroom/index.html");
});
//HTTP3000
http.listen(3000, function(){
console.log("listening on *:3000");
});
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "jade");
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// render the error page
res.status(err.status || 500);
res.render("error");
});
module.exports = app;
the following is the index.html of the entire file root
<body>
<div id="app"></div>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script type="text/javascript">
var socket = io.connect("http://localhost:3000")
console.log(socket)
socket.on("connect_error",(data)=>{
console.log(data + " - error");
})
socket.on("disconnect",function(data){
//
console.log(data);
})
``
socket:
Socket
acks
:
{}
connected
:
false
disconnected
:
true
ids
:
0
:Error: server error - error