i'm new here. Just starting with socket.io and node.js, and I tried to upload the project to a subfolder on my domain, but i can't find anything about subdirectories and socket.io (I saw some questions, but it didn't work for me) Thie domain is "http://www.nicolasrivero.com/2" It's working on localhost. Here is my code...
The console log error is this
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)
I think it's obvius, socket.io is trying to connect with nicolasrivero.com and not with nicolasrivero.com/2 ... Any help would be appreciated (Sorry for my bad english, lol)
Index.html (head)
<script src="/socket.io/socket.io.js"></script>
Index.html (body)
<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>
Aaaand in my server.js
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.');
});
});
socket.io connects to a server, not to a specific path. There is no such thing as connecting to http://www.nicolasrivero.com/2. socket.io connects to your server.
FYI, the URL it is using http://nicolasrivero.com/socket.io/?EIO=3&transport=polling&t=1429923179530-0 looks like a normal socket.io URL. All webSocket connections start with an http request and this is the type of http request that socket.io registers a handler for in order to field requests for webSocket connections.
If that request is returning a 404, then that is either because your web server is not running correctly or because socket.io is not initialized correctly with the web server. You can obviously see if the web server is operating correctly by checking other routes on the web server to see if they are working or not or by putting some debug info in your http request handler.
When socket.io is initialized correctly, it configures a handler in your web server for the /socket.io route and handles any incoming requests that target that route.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With