socket.io nodejs client show“GET http:// localhost:8888 / socket.io / 1 /?t = 1342788870007 404(Not Found)”

时间:2012-07-20 13:06:50

标签: node.js socket.io

客户端错误是:

GET http://localhost:8888/socket.io/1/?t=1342788870007 404 (Not Found)socket.io.js:1632

Socket.handshake socket.io.js:1632
Socket.connect socket.io.js:1671
Socket socket.io.js:1530
io.connect socket.io.js:91

(匿名函数)

我的客户js:

var socket = new io.connect("http://localhost", 8888);

socket.on("chatRoom", function(data){
  $("#log").html($("#log").html() + "<br>" + data);
})

$("#chatform").submit(function (event) {
  event.preventDefault();
  socket.emit('chatRoom', $("#chat").val())
})

我的服务器是:

var express = require('../node_modules/express'),
app = express.createServer(),
io = require('../node_modules/socket.io').listen(app),
fs = require('fs');

app.use(express.bodyParser());

 var port = 8888;

 // get html page ok
app.get('/html/*', function (req, res) {
    res.sendfile(__dirname + '/html/' + req.params[0]);
});

// chat
io.sockets.on('connection', function (socket) {
  socket.on("login", function(message){
    socket.emit('chatRoom', "sombody connect");
  })

  socket.on("chatRoom", function(data){
      socket.emit('chatRoom',"from server");
  })
})

app.listen(port);
谁能告诉我有什么不对? 我用户在服务器中表达+ socket.io,我在客户端使用socket.io.js 服务器和客户端都在localhost

2 个答案:

答案 0 :(得分:0)

正如@ebohlman所说

尝试使用

var socket = io.connect('http://localhost:8888');

在您的客户端js而不是

var socket = new io.connect("http://localhost", 8888);

答案 1 :(得分:0)

这是您的问题(在服务器端代码中):

app.listen(port);

您需要创建一个单独的HTTP服务器:

...
var app    = express();
var server = require('http').createServer(app);
var io     = require('../node_modules/socket.io').listen(server);
...
server.listen(port);