node.js socket.io 与 express 集成出错
PHPz
PHPz 2017-04-17 11:28:12
0
2
594

浏览器端

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"}

这是什么情况?求大神解答~~~

PHPz
PHPz

学习是最好的投资!

reply all(2)
迷茫

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 socket.io 0.9.x version of the browser-side js yourself, and then initiated the ws request in the browser
The server socket.io version is 1.x, these two versions are not 不通用 at all!

The

0.9.x version will first try to establish a websocket connection. The address format is /socket.io/1/?t=xxx. If the server response times out or refuses, it will fallback to polling. The data format of the server response is sid:interval_time:timeout_time:.... , will not produce a response in the json format

1.x version will give priority to create polling request, the address format is /socket.io/?transport=polling&t=xxx, and then upgrade to websocket, the server will return the configuration file in json 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 directly

<script src="/socket.io/socket.io.js"></script>
刘奇

The client uses this:

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

Delete the port

Also, what version of socket.io are you using? It is now 1.0, please remember to update. . .

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template