我知道这个问题已被多次询问我花了3个小时试图找出它但无济于事。我在GET中遇到错误 http://localhost:3000/socket.io/socket.io.js net :: ERR_ABORTED 和io没有定义。任何帮助将受到高度赞赏。我正在输入额外的coz我的帖子需要更多细节..........
public/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="styles.css" />
</head>
<body>
<div id="wrapper">
<div class="form_div" id ="login">
<p class="form_label">LOGIN FORM</p>
<form method="post" action="">
<p><input id ="user-name" type="text" placeholder="Enter user-name"></p>
<p><input id = "pass-word" type="password" placeholder="**********"></p>
<p><input id = "submit" type="submit" value="LOGIN"></p>
</form>
</div>
<br>
<br>
<br>
<div class="form_div" id = "signup">
<p class="form_label">SIGNUP FORM</p>
<form method="post" action="">
<p><input id ="signupusername" type="text" placeholder="Enter
Name"></p>
<p><input id = "signuppass" type="password"
placeholder="**********"></p>
<p><input id ="register" type="submit" value="SIGNUP"></p>
</form>
</div>
</div>
<script src = "index.js"></script>
<script src = "/socket.io/socket.io.js"></script>
public/index.js
var socket = io();
var loginDiv = document.getElementById('login');
var Username = document.getElementById('user-name');
var loginPassword = document.getElementById('pass-word');
var login = document.getElementById('submit');
var SignUpDiv = document.getElementById('signup');
var Signupuser = document.getElementById('signupusername');
var Signuppass = document.getElementById('signuppass');
var signup = document.getElementById('register');
login.onclick = function(){
socket.emit('login-details',{
username:Username.value,
password:loginPassword.value
});
}
signup.onclick = function(){
socket.emit('signup-details',{
newuser:Signupuser.value,
newuserpassword:Signuppass.val
});
}
socket.on('login-response',(data)=>{
if(data.success){
alert('login Successful');
}
else{
alert('login Unsuccessful');
}
});
socket.on('signup-response',(data)=>{
if(data.success){
alert('Signup Successful')
}
else{
alert('Signup-Successful')
}
});
server/server.js
var express = require('express');
var path = require('path');
var http = require('http');
var MongoClient = require('mongodb').MongoClient;
const socketIO = require('socket.io');
var app = express();
var server = http.createServer(app);
const port = process.env.PORT ||3000;
var io = socketIO(server);
const pathjoin = path.join( __dirname ,'../public');
app.use(express.static(pathjoin));
MongoClient.connect('mongodb://localhost:27017/DETAILS',(err,client)=>{
if(err){
return console.log('unable to connect to the MongoDb server');
}
const db =client.db('DETAILS');
console.log('connected to the MongoDb server');
});
io.on('connection',(socket)=>{
console.log('new user connected');
//login
socket.on('login-details',(data)=>{
db.collection('user-
details').find({username:data.username,password:data.password},
(err,result)=>{
if(err) throw err;
socket.emit('login-response',{
success:true
});
});
});
//signup
socket.on('signup-details',(data)=>{
db.collection('user-
details').insert({username:data.newuser,password:data.newuserpassword},
(err,result)=>{
if(err) throw err;
socket.emit('signup-response',{
success:true
});
});
});
});
app.listen(port,()=>{
console.log('server started');
});
答案 0 :(得分:0)
试试这个。变化,
<script src = "/socket.io/socket.io.js"></script>
到
<script src="http://YOUR_IP_ADDRESS/socket.io/socket.io.js"></script>
答案 1 :(得分:0)
在index.html中
<script src = "index.js"></script>
<script src = "/socket.io/socket.io.js"></script>
将其更改为
<script src = "/socket.io/socket.io.js"></script>
<script src = "index.js"></script>
在html中交换包含。首先需要包含socket.io,然后在index.js中使用它。
在index.js
中var socket = io.connect();
答案 2 :(得分:0)
错误解决了。这是一个愚蠢的错误!因为socket.io使用 http 服务器而不是快速服务器。所以我确实创建了一个 http 服务器,但是在收听端口时我使用了像app.listen()
这样的快递。在server / server.js中应该是:
server.listen(port,()=>{ console.log('server started');});
答案 3 :(得分:0)
在 server / server.js 中,在代码的最后,将app.listen()
更改为server.listen()
,因为express要求您实例化 HTTP 服务器在Socket.IO中。
这应该是 server.js
的新结尾部分 server.listen(port,()=>{
console.log('server started');
});