代码拉取完成,页面将自动刷新
同步操作将从 一天前/ChatServe 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
/**
* 作者: liushun
* 时间: 2020/5/17
* 功能: 程序启动
* **/
import express from 'express';
import chalk from 'chalk';
import config from './config';
import router from './routes';
import './mongodb/db.js';
import bodyParser from 'body-parser'
import socket from 'socket.io'
import mongoose from 'mongoose'
import Socket from './models/socket'
import Message from './models/message'
import loger from './util/log'
import moment from 'moment'
import './redis/redis'
moment.locale('zh-cn');
const app = express();
// 服务器提交的数据json化
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
//验证
router(app);
//声明静态资源地址
app.use(express.static('image'));
//声明静态资源地址
app.use(express.static('voice'));
//声明静态资源地址
app.use(express.static('public'));
app.set('showStackError', true)
mongoose.set('debug', true)
const server = app.listen(config.port, () => {
console.log(
chalk.green(`成功监听端口:${config.port}`)
)
});
const io = socket(server);
global.io = io;
io.on('connect', (socket)=>{
console.log("socket connect");
socket.on('login',async (user, fn)=>{
let userId = user.id
let socketId = socket.id
let socketModal = {
socketId,
userId
}
const socketRes = await Socket.findOne({userId});
try{
if(!socketRes){
let socket = new Socket(socketModal);
await socket.save();
}else{
await Socket.updateOne({userId},{socketId})
}
}catch(e){
fn('连接服务器失败')
}
fn('连接服务器成功')
})
socket.on('message', async (message, fn) => {
fn(message);
const {fromName, fromId, avatar, toName, toId, type, text, roomId, duration} = message;
const messageId = await Message.find({'roomId': roomId}).count()
console.log(messageId)
let mess = {
username: fromName,
userId: fromId,
avatar,
msg: text,
type: type,
roomId,
messageId,
duration
}
const messModal = await new Message(mess).save()
const selfSockets = await Socket.findOne({userId: fromId});
// console.log(selfSockets)
io.to(selfSockets.socketId).emit('message', messModal);
const friendSockets = await Socket.findOne({ userId: toId });
io.to(friendSockets.socketId).emit('message', messModal);
})
})
io.on('disconnect', (socket)=>{
console.log("socket disconnect");
})
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。