我在我的服務器上創建一個簡單的socket.io項目,但我有錯誤 我的工作是我的ip是http://5.61.25.90/~socket 所有文件都存儲在public_html URL :http://5.61.25.90/~socket/public_htmlsocket.io不工作在服務器上,並有404錯誤
&這裏是我的代碼
<!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)
兄弟我測試你的答案,但不能工作在哪個端口是您的節點服務器上運行 –
?如果它不是80端口,你需要在端口3000的URL中指定它,例如'http:// localhost:3000'。 –
我這樣做,但是我得到這個標題的錯誤 –