【发布时间】:2018-05-14 11:04:22
【问题描述】:
我使用 socket.io 最新版本发现了这个错误:“index.js: 83 POST http: //localhost/socket.io/?EIO = 3 & transport = polling & t = MDUHEO9 404 (Not Found)”。 我明白原因:真实地址一定是http://localhost:3000/socket.io/。 你知道我该如何纠正吗?
我看了很多讨论,但没有人有适合2.1.0版本的解决方案,即使在讨论中我读到降级提案,我也想避免它。
client.js:
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
var socket = io().connect('http://localhost:3000');
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
});
</script>
server.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){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
【问题讨论】:
-
你能证明你是在你的html中添加
socket.iojs文件吗?以及您如何从后端提供服务 -
@GeorgeBailey 感谢您的关注:我添加了代码。
标签: javascript node.js socket.io