I am trying to make a browser multiplayer game using Socket.IO and Express, although it's not very clear for me why Express is needed. I have the app in my server, in a folder called /game so to access the app, the user should type http://www.example.com/game instead of http://www.example.com:3000 which is how the tutorial is doing it. I know this is more of an Apache 2 VirtualHost configuration file issue but I didn't know where to post it. Here is my actual .conf file
# /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/example.com/public_html
ServerName example.com
ServerAlias www.example.com
ProxyPreserveHost On
ProxyPass /game http://www.example.com:3000
ProxyPassReverse /game http://www.example.com:3000
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Actually, when an user enters by typing the url with the port ( http://www.example.com:3000 ), the server returns a log message like 'user connected' BUT when I enter by typing the url I want ( http://www.example.com/game ) then it doesn't returns any message which is driving me crazy... index.js code:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('chat message', function(msg){
onsole.log('message: ' + msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
index.html code:
<!doctype html>
<html>
<head>
<title>Socket.IO App</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font: 13px Helvetica, Arial;
}
form {
background: #000;
padding: 3px;
position: fixed;
bottom: 0;
width: 100%;
}
form input {
border: 0;
padding: 10px;
width: 90%;
margin-right: .5%;
}
form button {
width: 9%;
background: rgb(130, 224, 255);
border: none;
padding: 10px;
}
#messages {
list-style-type: none;
margin: 0; padding: 0;
}
#messages li {
padding: 5px 10px;
}
#messages li:nth-child(odd) {
background: #eee;
}
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
});
</script>
</body>
</html>
Thanks in advance.
edit: Also, I have checked the browser console while accesing via example.com/game and it says the socket.io.js file is not found, but when entering the other way it does load the file correctly...