the client error is :
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
(anonymous function)
my client 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())
})
and my server is:
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);
who can tell me what is the wrong? i user express+socket.io in server, and i use socket.io.js in client both server and client are in localhost
As @ebohlman mentioned
Try using
var socket = io.connect('http://localhost:8888');
in your client js instead of
var socket = new io.connect("http://localhost", 8888);
This is your issue (in the server-side code):
app.listen(port);
You need to create a separate HTTP server instead:
...
var app = express();
var server = require('http').createServer(app);
var io = require('../node_modules/socket.io').listen(server);
...
server.listen(port);