【发布时间】:2021-04-03 07:38:04
【问题描述】:
我正在(尝试)在 Heroku 上运行一个聊天应用程序。我是一名前端人员,试图培养我的后端技能,所以我知道这对我来说有点吃力,但我在部署 Heroku 之前在本地测试了该应用程序。将它部署到 Heroku 后,我请几个朋友登录和注销并帮助我解决问题。它工作得很好。快进到今晚,当应用程序处理来自陌生人的实际流量(作为假日马拉松的一部分)的时候......它崩溃了。几乎立刻。
这是 Heroku 错误日志:
heroku[web.1]: State changed from starting to up
[web.1]: /app/server.js:44
[web.1]: io.to(user.room).emit('message', formatMessage(user.username, msg));
[web.1]: ^
[web.1]:
[web.1]: TypeError: Cannot read property 'room' of undefined
[web.1]: at Socket.<anonymous> (/app/server.js:44:16)
[web.1]: at Socket.emit (events.js:314:20)
[web.1]: at Socket.onevent (/app/node_modules/socket.io/dist/socket.js:253:20)
[web.1]: at Socket._onpacket (/app/node_modules/socket.io/dist/socket.js:216:22)
[web.1]: at /app/node_modules/socket.io/dist/client.js:205:28
[web.1]: at processTicksAndRejections (internal/process/task_queues.js:79:11)
[web.1]: Process exited with status 1
[web.1]: State changed from up to crashed
好的,我知道这里发生了一些不确定的问题 - 尽管我的 Node/JS 技能有限,我很难在源代码上快速归零 - 但这个错误不会导致应用程序变得简单根本不工作,而不是一开始工作,然后在遇到任何实际流量时立即崩溃?
我想让这个应用程序运行良好,所以如果有比我更好的 JS 排骨的人可以在这里帮助我,我将不胜感激。
这是我的 server.js 文件:
const path = require('path');
const http = require('http');
const express = require('express');
const socketio = require('socket.io');
const formatMessage = require('./utils/messages');
const { userJoin, getCurrentUser, userLeave, getRoomUsers } = require('./utils/users');
const app = express();
const server = http.createServer(app);
const io = socketio(server);
// Set static folder
app.use(express.static(path.join(__dirname, 'web_apps--listener_chat')));
const botName = '???? Jon Solobot ????';
// Run when a client connects
io.on('connection', socket => {
socket.on('joinRoom', ({ username, room }) => {
const user = userJoin(socket.id, username, room);
socket.join(user.room);
// Welcome current user
socket.emit('message', formatMessage(botName, `Welcome to the WPRBXmas Listener Chat, ${user.username}!`));
// Broadcast when a user connects
socket.broadcast
.to(user.room)
.emit('message', formatMessage(botName, `${user.username} has entered the chat, bearing tidings of comfort & oi.`));
// Send users and room info.
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room)
});
});
// Listen for chatMessage
socket.on('chatMessage', (msg) => {
const user = getCurrentUser(socket.id);
io.to(user.room).emit('message', formatMessage(user.username, msg));
});
// Broadcast when a user disconnects
socket.on('disconnect', () => {
const user = userLeave(socket.id);
if(user) {
io.to(user.room).emit('message', formatMessage(botName, `${user.username} has left the chat for a long winter's nap.`)
);
// Send users and room info.
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room)
});
}
});
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));
这是我的 users.js 文件:
const users = [];
// Join user to chat
function userJoin(id, username, room) {
const user = { id, username, room};
users.push(user);
return user;
}
// Get current user
function getCurrentUser (id) {
return users.find(user => user.id === id);
}
// User leaves chat
function userLeave(id) {
const index = users.findIndex(user => user.id === id);
if(index !== -1) {
return users.splice(index, 1)[0];
}
}
// Get room users
function getRoomUsers (room) {
return users.filter(user => user.room === room);
}
module.exports = {
userJoin,
getCurrentUser,
userLeave,
getRoomUsers
};
这是我的 messages.js 文件:
const moment = require('moment');
function formatMessage(username, text) {
return {
username,
text,
time: moment().format('h:mm a')
};
}
module.exports = formatMessage;
我知道这很多,但我猜对 Socket.io 更熟悉的人将能够看到这个并立即发现问题。
非常感谢花时间阅读和回复的任何人!节日快乐!
【问题讨论】:
标签: javascript node.js heroku socket.io