【发布时间】:2017-10-21 23:56:59
【问题描述】:
我正在尝试使用 Socket.IO 和 Express 制作一个浏览器多人游戏,尽管我不太清楚为什么需要 Express。我的服务器中有该应用程序,位于名为 /game 的文件夹中,因此要访问该应用程序,用户应键入 http://www.example.com/game 而不是 http://www.example.com:3000,这就是教程的操作方式。我知道这更多是 Apache 2 VirtualHost 配置文件问题,但我不知道在哪里发布。 这是我的实际 .conf 文件
# /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
ServerAdmin admin@example.com
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>
实际上,当用户通过输入带有端口 (http://www.example.com:3000) 的 url 进入时,服务器会返回一条日志消息,例如“用户已连接”,但是当我通过输入我想要的 url (http://www.example.com/game) 输入时,然后它不返回任何让我发疯的消息... index.js 代码:
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 代码:
<!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>
提前致谢。
edit:另外,我在通过 example.com/game 访问时检查了浏览器控制台,它说未找到 socket.io.js 文件,但是当以其他方式输入时正确加载文件...
【问题讨论】:
-
嗨@MirzaSisic 感谢您的回答!它实际上帮助了我,但我已经看过那篇文章,它并没有解决我的问题。现在我可以运行我的应用程序并通过 example.com:3000 访问它,但我希望能够通过 example.com/game 访问它。实际上,他们都将我返回到正确的页面,但是当通过 example.com/game 进入时,它不会加载 socket.io 脚本(404)并且服务器不会返回任何消息作为“用户已连接”。再次感谢!
-
找不到socket.io时,它记录的url是什么?并确认这是否是您的套接字文件的正确 url
-
我之前使用的是 CDN url。现在我已将其更改为 src="socket.io/socket.io.js",现在在请求 example.com/game 时我没有收到有关 js 文件加载的任何错误,但我收到 404“polling-xhr.js”。 js:264 GET example.com/socket.io/… 404(未找到)"
标签: javascript node.js apache socket.io reverse-proxy