codestates / codestates/NERDchat
[Error Handling] Socket.io Dynamic Namespace
- Dominant language
- No language data
- Stars
- 0
- Forks
- 1
- PR merge metrics
- No merged PRs in 30d
Description
## 어떤 에러인가요?
* 프로젝트 특성상 네임스페이스가 동적으로 생성되야 하는데, 작동하지 않았다.
```
$ no error code
```
## 에러 핸들링 방법
```
// Server Code
io.of(/^\/\w+$/).on('connection', (socket) => {
const ns = socket.nsp // namespace.name
ns.on('connection', (socket) => {
socket.on('joinRoom', roomName => { // namespace => room에서 요청하는 경우
socket.join(roomName)
console.log('join!')
console.log(ns.adapter)
})
})
socket.on("getRoomList", () => { // 특정 namespace에서 요청하는 경우
console.log(ns.adapter.rooms) // namespace안의 room들을 볼수있다.
console.log(ns.adapter.sids) // namespace안 sid
ns.emit("getRoomList")
})
})
// Client Code
// Namespace.js
useEffect(() => {
const socket = io(process.env.REACT_APP_HOST + `/${namespace}`)
socket.emit("getRoomList")
socket.on("getRoomList", (lists) => {
console.log(lists)
})
return () => socket.close()
}, [])
return (
Join Room
)
// chatRoom.js
useEffect(() => {
const socket = io(process.env.REACT_APP_HOST + `/${namespace}`, {
query: {roomName, namespace}
})
socket.emit('joinRoom', roomName)
return () => socket.close()
})
```
namespace를 정규식으로 표현했는데 아직 수정해야할 부분들이 많다.
## 에러 핸들링을 위해 참고한 레퍼런스 링크
[Link](https://www.alxolr.com/articles/working-with-socket-io-dynamic-namespaces)
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.