-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from DucHuy2801/chat
Chat group
- Loading branch information
Showing
6 changed files
with
282 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict' | ||
|
||
const express = require("express") | ||
const router = express.Router() | ||
const { asyncHandler } = require('../../auth/checkAuth') | ||
const messageController = require("../../controllers/message.controller") | ||
|
||
router.post("/", asyncHandler(messageController.createMessage)) | ||
router.delete("/", asyncHandler(messageController.deleteMessage)) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,63 @@ | ||
const io = require("socket.io")(8900, { | ||
const app = require("../app"); | ||
const Group = require("../models/group.model") | ||
const GroupUser = require("../models/group_member.model"); | ||
|
||
const server = require("http").createServer(app) | ||
const io = require("socket.io")(server, { | ||
cors: { | ||
origin: `http://localhost:${process.env.CLIENT_PORT}` | ||
origin: 'http://localhost:3000', | ||
METHODS: ["POST", "GET"], | ||
Credential: true | ||
} | ||
}) | ||
|
||
// using socket | ||
const onlineUsers = []; | ||
io.on("connection", (socket) => { | ||
console.log("A user connected") | ||
console.log("A connection has been made"); | ||
// store in users array in online | ||
socket.on("online", (userID) => { | ||
onlineUsers.push({ userId: userID, socketId: socket.id }) | ||
}) | ||
|
||
// add member to group | ||
socket.on("add ember", async (userID) => { | ||
const target = onlineUsers.find((user) => user.userId == userID) | ||
|
||
if (target) { | ||
const groupIDs = await GroupUser.findAll({ | ||
attributes: ["group_id"], | ||
where: { user_id: userID } | ||
}) | ||
|
||
const allGroups = []; | ||
for (let i = 0; i < groupIDs.length; i++) { | ||
allGroups.push(groupIDs[i].group_id); | ||
} | ||
|
||
const groups = await Group.findAll({ | ||
attributes: ["group_id", "name", "description"], | ||
where: { group_id: allGroups }, | ||
}); | ||
|
||
console.log(target.socketId); | ||
io.to(target.socketId).emit("groupData", groups); | ||
} | ||
}) | ||
|
||
// make user join a room | ||
socket.on("join", function (room) { | ||
socket.join(room); | ||
}); | ||
|
||
// broadcast the message to a room | ||
socket.on("send message", (msg, room) => { | ||
io.in(room).emit("room message", msg); | ||
}); | ||
|
||
// on disconnection | ||
socket.on("disconnect", () => { | ||
console.log("A disconnection has been made"); | ||
}); | ||
|
||
}) |