我尝试使用NodeJS,Express ans socket.io
进行一些聊天使用localhost,log4js进行调试我在本地工作正常,socket.io正常工作没有问题。
但是当我尝试将其部署在远程服务器上时,它无法正常工作。 这是一个链接https://cryptic-fjord-64553.herokuapp.com/ Ofc,我用Google搜索,写了类似的主题,尝试了很多东西,但它没有用。
无法加载资源:服务器响应状态为404
engine.io-client:socket socket error {" type":" TransportError"," description":404}
或
engine.io-client:socket socket error {" type":" TransportError"," description":0}
我的服务器,app.js包括:
var express = require('express'),
app = express(),
logger = require('log4js').getLogger();
server = require('http').Server(app),
io = require('socket.io')(server),
当我试图在远程服务器(heroku)上部署我的应用程序时,这个注释的块而不是server = ..和io = ..上面
/*
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Headers", "Content-Type");
res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS");
next();
});
var https = require('https'),
socketio = require('socket.io'),
server = https.createServer(app),
io = socketio.listen(server, {log:false, origins:'*:*'});
app.set('port', (process.env.PORT || 5000));
*/
当我尝试在heroku上部署app时,我会评论server.listen .. block下面的
var pug = require('pug'),
port = 4000;
server.listen(port, '127.0.0.1', ()=>{
var addr = server.address();
logger.level = 'debug';
logger.debug('listening on ' + addr.address + ':' + addr.port);
});
app.use(express.static(__dirname + '/public'));
app.get('/socket.io.js', (req,res)=>{
res.sendFile(__dirname + '/node_modules/socket.io-client/dist/socket.io.js');
});
app.get('/socket.io/socket.io.js', (req,res)=>{
res.sendFile(__dirname + '/node_modules/socket.io-client/dist/socket.io.js');
});
app.get('/jquery.js', (req,res)=>{
res.sendFile(__dirname + '/node_modules/jquery/dist/jquery.min.js');
});
app.set('views', __dirname + '/views');
app.set('view engine', 'pug');
const amazingStuff = 'Its amazing stuff, ',
date = new Date;
app.get('/', (req,res)=>{
res.render('pages/index', {
title: 'main page',
message: date,
showChat: false
});
io.on('connection', (socket)=>{
socket.on('newUserJoin', (userName)=>{
socket.session = {};
socket.session.userName = userName;
socket.session.address = socket.handshake.address;
socket.session.id = socket.id;
socket.broadcast.emit('newUser', socket.session);
socket.emit('user', socket.session)
socket.emit('userLisr', io.length)
logger.info('user ' + socket.session.userName + ' / ip ' + socket.session.address)
logger.info('user count: ' + io.engine.clientsCount)
logger.debug(socket.session)
var clients = io.sockets.connected,
clientList = {};
for (var i in clients){
if(clients[i].session) clientList[i] = clients[i].session;
}
socket.emit('clientList', clientList);
})
//get a new message and share it to all users
socket.on('newMessage', (message)=>{
socket.broadcast.emit('shareMessage', message)
})
})
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
版本:
"devDependencies": {
"nodemon": "^1.11.0"
},
"dependencies": {
"express": "^4.15.3",
"jquery": "^3.2.1",
"log4js": "^2.3.3",
"pug": "^2.0.0-rc.3",
"socket.io": "^2.0.3"
}
client,main.js:
var socket = io();
//var socket = io.connect('https://cryptic-fjord-64553.herokuapp.com/');
和处理程序
这里是在html(pug)中添加脚本,首先在本地工作,评论的东西是我尝试在heroku上部署的尝试,但它不起作用。
script(src='/socket.io/socket.io.js')
//-script(src='/socket.io.js')
//-script(src='https://cryptic-fjord-64553.herokuapp.com/socket.io.js')
你无法打开上面的链接,看看控制台中发生了什么。没有关于客户端脚本的警告,我的意思是main.js文件,所以它可以连接到服务器,main.js知道什么是' io'。因此,似乎可以通过html和socket.io连接到服务器的链接。所以可能是服务器端app.js中的问题
如何强制它在heroku上工作?