add socker.io fore service-private
This commit is contained in:
parent
6818e0bb47
commit
0625a3929f
4 changed files with 35 additions and 3 deletions
|
|
@ -1,6 +1,7 @@
|
|||
const config = {
|
||||
mongodbDatabase: 'chat',
|
||||
mongodbHost: 'mongodb://127.0.0.1:27018/',
|
||||
mongodbHost: 'mongodb://127.0.0.1:27017/',
|
||||
// mongodbHost: 'mongodb://127.0.0.1:27018/', //when commit
|
||||
charset: 'utf8',
|
||||
mongodbLogin: '',
|
||||
mongodbPassword: '',
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
const io = require("socket.io")
|
||||
|
|
@ -20,8 +20,10 @@ router.get("/:userId", async (req, res) => {
|
|||
try {
|
||||
const conversation = await Conversation.find({
|
||||
members: { $in: [req.params.userId] },
|
||||
// members: { $eq: [req.params.userId, "yuki"]},
|
||||
|
||||
});
|
||||
res.status(200).json(conversation);
|
||||
res.status(200).json(conversation[0]["_id"]);
|
||||
}catch (err){
|
||||
res.status(500).json(err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ const path = require("path");
|
|||
const config = require("./config");
|
||||
|
||||
const Conversations = require("../service-privateroom/models/Conversation");
|
||||
const Messages = require("../service-privateroom/models/Message");
|
||||
const url = config.mongodbHost+config.mongodbDatabase;
|
||||
|
||||
mongoose.connect(url,({useNewUrlParser: true, useUnifiedTopology: true})).then( function(){
|
||||
|
|
@ -55,6 +56,35 @@ io.on('connection',socket => {
|
|||
console.log(`${getUsername} joined the chat.`);
|
||||
|
||||
//TODO apply conversations and messages
|
||||
socket.on('privateroom',function(data){
|
||||
const sender = data.sender;
|
||||
const receiver = data.receiver;
|
||||
const date = data.date;
|
||||
const message = data.message;
|
||||
|
||||
// get conversationid
|
||||
const conversation = async () => {
|
||||
const result = await Conversations.find({
|
||||
members: {$eq: [sender, receiver]},
|
||||
});
|
||||
return result;
|
||||
}
|
||||
const conversationId = conversation[0]["_id"];
|
||||
|
||||
Messages.insertMany([{
|
||||
conversationId: conversationId,
|
||||
sender: sender,
|
||||
text: message,
|
||||
date: date
|
||||
}
|
||||
]).then(function(){
|
||||
console.log(data, "inserted");
|
||||
socket.broadcast.emit(conversationId,[data]);
|
||||
socket.emit(conversationId,[data]);
|
||||
}).catch(function(error){
|
||||
console.log("error",error);
|
||||
});
|
||||
});
|
||||
|
||||
socket.on("disconnect", function() {
|
||||
console.log(`${getUsername} left the chat.`);
|
||||
|
|
|
|||
Reference in a new issue