var socket = io.connect('http://localhost:3000');
var express = require('express');
var routes = require('./routes/index');
var user = require('./routes/user');
var chat = require('./routes/chat');
var http = require('http');
var path = require('path');
var MongoStore = require('connect-mongo')(express);
var settings = require('./settings');
var flash = require('connect-flash');
var multer = require('multer');
var app = express();
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(flash());
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({
secret:settings.cookiesSecret,
key:settings.db,
cookie:{maxAge:1000*60*60*24*30},
store:new MongoStore({
db:settings.db
})
}));
app.use(multer({
dest: './public/images/user',
rename: function (fieldname, filename) {
return filename;
}
}));
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname,'bower_components')));
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
routes(app);
var server = http.createServer(app);
var io = require('socket.io')(server);
io.on('connection', function(){ console.log('connection') });
server.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
出错这个错误:
Request URL:http://localhost:3000/socket.io/1/?t=1420622609420
Status Code:400 Bad Request
服务器返回的错误信息是:{"code":0,"message":"Transport unknown"}
这是什么情况?求大神解答~~~
The code is not complete. What is the version of socket.io and what is the version of express?
I conclude that you must have copied a
Thesocket.io 0.9.x
version of the browser-side js yourself, and then initiated the ws request in the browserThe server
socket.io
version is1.x
, these two versions are not不通用
at all!0.9.x
version will first try to establish awebsocket
connection. The address format is/socket.io/1/?t=xxx
. If the server response times out or refuses, it will fallback topolling
. The data format of the server response issid:interval_time:timeout_time:...
. , will not produce a response in thejson
format1.x
version will give priority to createpolling
request, the address format is/socket.io/?transport=polling&t=xxx
, and then upgrade towebsocket
, the server will return the configuration file injson
format. If an error occurs, the returned json format is{"code":0,"message":"xxx"}
So you used version 0.9.x client socket.io to initiate a request to version 1.x server socket.io. The solution is to use
socket.io内置的js
, the address is/socket.io/socket.io.js
, this js is not needed You can copy it yourself and quote it directlyThe client uses this:
Delete the port
Also, what version of socket.io are you using? It is now 1.0, please remember to update. . .