如何解决GET http://localhost:3306/socket.io/?EIO=3&transport=polling&t=NQfLxAy net::ERR_INVALID_HTTP_RESPONSE
我正在使用 Node.js 构建一个实时聊天框。所以现在我只想将用户输入的名称显示到控制台中。但它不会那样做。请帮我解决这个问题,我将不胜感激。
index.PHP
<!-- include jquery and socket IO -->
<script src="js/jquery.js"></script>
<script src="js/socket.io.js"></script>
<!-- create a form to enter username -->
<form onsubmit="return enterName();">
<input type="text" name="name" id="name" placeholder="Enter name">
<input type="submit" name="submit">
</form>
<script>
res.setHeader('Access-Control-Allow-Origin',"localhost:3000");
//creating io instance
var socket = io("http://localhost:3000");
function enterName(){
//get username
var name = document.getElementById("name").value;
//send it to server
socket.emit("user_connected",name);
//prevent the form from submiting
return false;
}
//listen from server
socket.on("user_connected",function (username){
console.log(username);
});
</script>
server.js
//creating express instance
var express = require("express");
var app = express();
//creating http instance
var http = require("http").createServer(app);
//creating socket io instance
var io = require("socket.io")(http);
io.on("connection",function (socket){
console.log("User connected",socket.id);
// attach incoming listener for new user
socket.on("user_connected",function (username) {
// save in array
users[username] = socket.id;
// socket ID will be used to send message to individual person
// notify all connected clients
io.emit("user_connected",username);
});
});
//start the server
http.listen(3000,function(){
console.log("Server started");
});
问题出在 index.PHP 中。请让我知道您认为问题可能出在哪里,如果可能,您也可以尝试我的 index.PHP 并对其进行编辑并使其工作。太感谢了! :)
解决方法
http.listen(3000,function(){
console.log("Server started");
});
和
var socket = io("http://localhost:3306");
似乎使用了不同的端口。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。