我尝试在我的网络服务器上学习socket.io。我在我的服务器上安装了node.js和socket.io。我已准备好网站我的socket javascript代码工作正常 - 在控制台上显示端口3000但是当我尝试运行示例聊天应用程序时,我得到了" socket.io/?EIO = 3& transport = polling& t = 1423949334574-31 404(未找到)"错误。错误在哪里?
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);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
有我的index.html脚本:
<script>
var socket = io("http://localhost:3000");
$('form').submit(function(){
socket.emit('chat message', $('#messagebox').val());
var mesaj = $('#messagebox').val();
$('.messageBoard').prepend('<p style="display: none">'+mesaj+'</p>');
$('#messagebox').val('');
$('.messageBoard').find('p:first-child').show('slideDown');
e.preventDefault();
return false;
});
socket.on('chat message', function(msg){
$('.messageBoard').prepend('<p style="display: none">'+mesaj+'</p>');
});
</script>