未加载客户端库js文件。试图将socket.io加载到我的项目中,但客户端没有检测到应该自动提供的javascript库。这是我的代码。
var express = require('express')
, passport = require('passport')
, util = require('util')
, session = require('express-session')
, SteamStrategy = require('../../').Strategy
, Steam = require('machinepack-steam')
, path = require('path')
, app = express()
, http = require('http')
, socket = require('socket.io')
;
app.set('port', process.env.PORT || 2053);
var server = http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
var io = socket.listen(server);
io.sockets.on('connection', function () {
console.log('hello world im a hot socket');
});
var steamkey = 'redacted';
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
passport.use(new SteamStrategy({
returnURL: 'https://get-a.team/auth/steam/return',
realm: 'https://get-a.team',
apiKey: steamkey
},
function(identifier, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
profile.identifier = identifier;
return done(null, profile);
});
}
));
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(session({
secret: 'your secret',
name: 'name of session id',
resave: true,
saveUninitialized: true}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join('public')));
app.get('/', function(req, res){
if(req.user){
res.redirect('/account');
} else {
res.render('index', { user: req.user });
}
});
app.get('/search/:appid', ensureAuthenticated, function(req, res){
if(Number.isInteger(+req.params.appid)){
res.render('search', { user: req.user, appid: req.params.appid });
} else {
res.render('error', { user: req.user, error: "Wrong url format "+req.params.appid });
}
})
app.get('/account', ensureAuthenticated, function(req, res){
Steam.getOwnedGames({
steamid: req.user.id,
key: steamkey,
include_appinfo: 1,
include_played_free_games: 1,
appids_filter: [],
}).exec({
// An unexpected error occurred.
error: function(err) {
console.error(err);
},
// OK.
success: function(result) {
//console.log(result);
// save every game that isnt already saved
res.render('account', { user: req.user, games: result });
},
});
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
app.get('/auth/steam',
passport.authenticate('steam', { failureRedirect: '/' }),
function(req, res) {
res.redirect('/');
});
app.get('/auth/steam/return',
passport.authenticate('steam', { failureRedirect: '/' }),
function(req, res) {
res.redirect('/account');
});
app.listen(3000);
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/');
}

非常感谢任何帮助
我已经尝试了很多不同的方法来在线包含套接字库但是没有工作,甚至直接从socket.io网站复制代码。
我尝试了localhost:2053 / socket.io / socket.io.js,它仍然无法工作。不确定我做错了什么
答案 0 :(得分:1)
想出来。不得不将socket.io重新加载到另一个端口:
...
, app2 = require('express')()
, http2 = require('http').Server(app2)
;
var io = require('socket.io')(http2);
io.on('connection', function(socket){
console.log('a user connected');
});
app2.get('/', function(req, res){
res.send('<h1>Hello world</h1>');
});
http2.listen(2085, function(){
console.log('listening on *:2085');
});
&#13;
创建一个nginx规则来从该端口提供lib:
location /route/ {
rewrite ^/route/?(.*)$ /$1 break;
proxy_pass http://127.0.0.1:2085;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
&#13;
最后从客户端加载它:
<script src="/route/socket.io/socket.io.js"></script>
如果有人知道这样做的正确方法,请告诉我。这虽然有效,但感觉就像在一个方孔中强迫一个圆钉。