[英]socket.io dont work on server and have 404 error
我在服務器中創建了一個簡單的socket.io項目,但我在該IP上運行的IP地址為http://5.61.25.90/~socket時出錯,所有文件均存儲在URL為http://5.61的 public_html中。 25.90 /〜socket / public_html
這是我的代碼
<!doctype html>
<html>
<head>
<title>Socket.IO chat</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; }
#messages { margin-bottom: 40px }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.4/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;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
window.scrollTo(0, document.body.scrollHeight);
});
});
</script>
</body>
</html>
上面的代碼是index.html文件,在底部有index.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(port, function(){
console.log('listening on *:' + port);
});
現在控制台面板中出現404錯誤
像這樣
未找到http://5.61.25.90/socket.io/?EIO=3&transport=polling&t=LqRzAYF 404 socket.io.js(第4948行)
我認為這可以幫助:
app.get('*', function(req, res) {
代替這個:
app.get('/', function(req, res) {
您在后端的應用路由器僅捕獲路由'/'
。 因此它無法處理與路由不完全匹配的請求。
看起來您正在服務器上使用Apache2虛擬主機。 如果我是正確的,請嘗試將這兩行添加到您的Apache配置文件中:
ProxyPass /socket.io/socket.io.js http://127.0.0.1:3000/socket.io/socket.io.js
ProxyPass /socket.io ws://127.0.0.1:3000/socket.io/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.