控制台日志错误是这个
http://nicolasrivero.com/socket.io/?EIO=3&transport=polling&t=1429923179530-0 Failed to load resource: the server responded with a status of 404 (Not Found)
我认为这是obvius,socket.io正试图与nicolasrivero.com联系,而不是与nicolasrivero.com/2联系......任何帮助都会受到赞赏(抱歉我的英文不好,哈哈)
Index.html(head)
<script src="/socket.io/socket.io.js"></script>
Index.html(正文)
<input autofocus id="name" placeholder="Nombre">
<textarea placeholder="caja de texto" id="area" style="width:220px; height:200px; background-color:white; font-family: "Times New Roman", Georgia, Serif;"></textarea>
<input autofocus id="msj" placeholder="Input name" onkeydown="if (event.keyCode == 13) document.getElementById('enviar').click()" >
<button id="enviar" type="button" onclick="enviarmsj()"> Enviar </button>
Index.html(js)
<script>
var socket = io.connect();
document.getElementById('area').disabled = true;
document.getElementById('msj').focus();
function enviarmsj(){
input = document.getElementById('msj').value;
nombre = document.getElementById('name').value;
console.log(input);
socket.emit('miMensajeEnviado', input, nombre);
document.getElementById('msj').value = "";
}
socket.on('mensajeEnviado', function(input, nombre){
console.log(input);
document.getElementById("area").innerHTML = document.getElementById("area").innerHTML + nombre + ": " + input + "\n";
});
</script>
我的server.js中的Aaaand
var serverPort = 3000;
var http = require('http').createServer(MyServer);
var fs = require('fs');
var io = require('socket.io').listen(http);
var nSight=0;
var contentTypes={
".html":"text/html",
".css":"text/css",
".js":"application/javascript",
".png":"image/png",
".jpg":"image/jpeg",
".ico":"image/x-icon",
".m4a":"audio/mp4",
".oga":"audio/ogg"
};
http.listen(3000);
function MyServer(request,response){
var filePath = '.' + request.url;
if (filePath == './')
filePath = './index.html';
var extname = filePath.substr(filePath.lastIndexOf('.'));
var contentType = contentTypes[extname];
if(!contentType)
contentType = 'application/octet-stream';
//console.log((new Date()) + ' Serving ' + filePath + ' as ' + contentType);
fs.exists(filePath, function(exists){
if(exists){
fs.readFile(filePath, function(error, content){
if(error){
response.writeHead(500, { 'Content-Type': 'text/html' });
response.end('<h1>500 Internal Server Error</h1>');
}
else{
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}
});
}
else{
response.writeHead(404, { 'Content-Type': 'text/html' });
response.end('<h1>404 Not Found</h1>');
}
});
}
io.sockets.on('connection', function(socket){
socket.player = nSight++;
console.log(socket.id + ' connected as player ' + socket.player);
socket.on('miMensajeEnviado', function(input, nombre){
io.sockets.emit("mensajeEnviado", input, nombre);
})
socket.on('disconnect', function(){
console.log('Player' + socket.player + ' disconnected.');
});
});
答案 0 :(得分:1)
socket.io
连接到服务器,而不是特定路径。没有连接到http://www.nicolasrivero.com/2
的东西。 socket.io连接到您的服务器。
仅供参考,它使用http://nicolasrivero.com/socket.io/?EIO=3&transport=polling&t=1429923179530-0
的网址看起来像普通的socket.io网址。所有webSocket连接都以http请求开头,这是socket.io注册处理程序的http请求类型,以便为webSocket连接发送请求。
如果该请求返回404,则可能是因为您的Web服务器未正确运行,或者因为未使用Web服务器正确初始化socket.io。您可以通过检查Web服务器上的其他路由来查看Web服务器是否正常运行,以查看它们是否正常工作,或者在http请求处理程序中添加一些调试信息。
正确初始化socket.io时,它会在Web服务器中为/socket.io路由配置一个处理程序,并处理任何以该路由为目标的传入请求。