const express = require('express')
const socketIo = require('socket.io')
const bodyParser = require('body-parser')
const cors = require('cors')
const cookieParser = require('cookie-parser')
require('dotenv').config()
const mongoose = require('mongoose')
const router = require('./router/index')
const errorMiddleware = require('./middlewares/error-middleware')
const http = require('http')
const WebSocket = require('ws')
const { handleWebSocketConnection } = require('./websockets/socket-controller')
const PORT = process.env.PORT || 5000
const app = express()
app.use(express.json())
app.use(cookieParser())
app.use(
cors({
credentials: true,
origin: process.env.CLIENT_URL,
})
)
app.use(bodyParser.json())
app.use('/api', router)
const server = http.createServer(app)
const wss = new WebSocket.Server({ server, path: '/api/messages' })
app.use(errorMiddleware)
wss.on('connection', handleWebSocketConnection)
const start = async () => {
try {
await mongoose.connect(process.env.DB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
server.listen(PORT, () => console.log(`Server started on PORT = ${PORT}`))
} catch (error) {
console.log(error)
}
}
start()
const handleWebSocketConnection = (ws) => {
console.log('New WebSocket connection')
ws.on('message', (message) => {
console.log(`Received WebSocket message: ${message}`)
})
ws.send('WebSocket connection established!')
}
module.exports = { handleWebSocketConnection }
const PORT = process.env.PORT || 5000
const app = express()
const server = http.createServer(app)
const wss = new ws.Server({ server: server, path: '/messages' })
server.on('upgrade', (request, socket, head) => {
wss.on('connection', (socket) => {
console.log('New WebSocket connection')
socket.on('message', (message) => {
console.log(`Received message: ${message}`)
})
})
})
const socket = new WebSocket('ws://localhost:5000/messages')
const profileList = [
{ title: 'Settings', icon: <Gear />, ref: createRef(null), },
{
title: 'Log out',
onClick: handleLogOut,
icon: <LogOut />,
ref: createRef(null),
},
]
const DropDown = (props) => {
const { icon, list } = props
const dropdownRef = useRef(null)
const [isDropDawnActive, setIsDropDawnActive] = useState(false)
useClickOutSide(dropdownRef, () => {
if (isDropDawnActive) setIsDropDawnActive(false)
})
const handleDropDawn = () => {
setIsDropDawnActive(!isDropDawnActive)
}
return (
<div className={styles.dropDawn} ref={dropdownRef}>
<div className={styles.dropDawnButton}>
<CustomButton
type={'button'}
icon={icon}
isScalabale={false}
onClick={handleDropDawn}
/>
</div>
{isDropDawnActive && (
<ul className={styles.dropDawnMenu}>
<TransitionGroup>
{list.map((item) => (
<CSSTransition
key={item.title}
nodeRef={item.ref}
timeout={1500}
classNames={{
enter: styles.dropDawnItemEnter,
enterActive: styles.dropDawnItemEnterActive,
exit: styles.dropDawnItemExit,
exitActive: styles.dropDawnItemExitActive,
}}
>
<li className={styles.dropDawnItem} onClick={item.onClick}>
{item.icon}
<span className={styles.dropDawnItemText}>{item.title}</span>
</li>
</CSSTransition>
))}
</TransitionGroup>
</ul>
)}
</div>
)
}
export default DropDown