create private namespaces

This commit is contained in:
data 2019-07-25 20:03:31 +01:00
parent 1e62608188
commit 9fd8ac7969
2 changed files with 41 additions and 14 deletions

View File

@ -22,6 +22,20 @@ module.exports = io => {
sendNamespaces() sendNamespaces()
}) })
}) })
socket.on('chat', id => {
const target = io.sockets.sockets[id]
if (!target || socket.id === target.id) {
console.log('Not starting chat.', socket, target)
return
}
console.log(`${socket.id} wants to chat with ${target.id}`)
// create a shared namespace
const namespace = `${socket.id.substr(0, 5)}.${target.id.substr(0, 5)}`
socket.join(namespace)
target.join(namespace)
sendNamespaces()
})
socket.on('part', () => { socket.on('part', () => {
console.log('part') console.log('part')

View File

@ -106,6 +106,11 @@ class App extends React.Component {
this.socket.emit('join', namespace) this.socket.emit('join', namespace)
this.setState({ namespace }) this.setState({ namespace })
} }
query(user) {
console.log(`starting query with ${user}`)
this.socket.emit('chat', user)
this.setState({ namespace: user })
}
sendMessage(msg) { sendMessage(msg) {
this.socket.emit('message', msg) this.socket.emit('message', msg)
} }
@ -152,11 +157,17 @@ class App extends React.Component {
</Dropdown.Toggle> </Dropdown.Toggle>
<Dropdown.Menu className="bg-dark"> <Dropdown.Menu className="bg-dark">
{this.state.allUsers.map(u => ( {this.state.allUsers
<Dropdown.Item key={u} className="bg-dark text-light"> .filter(u => u !== socket.id)
{u} .map(u => (
</Dropdown.Item> <Dropdown.Item
))} key={u}
className="bg-dark text-light"
onClick={() => this.query(u)}
>
{u}
</Dropdown.Item>
))}
</Dropdown.Menu> </Dropdown.Menu>
</Dropdown> </Dropdown>
<Dropdown> <Dropdown>
@ -165,15 +176,17 @@ class App extends React.Component {
</Dropdown.Toggle> </Dropdown.Toggle>
<Dropdown.Menu className="bg-dark"> <Dropdown.Menu className="bg-dark">
{this.state.namespaces.map(r => ( {this.state.namespaces
<Dropdown.Item .filter(n => n.namespace[0] === '#')
key={r.namespace} .map(r => (
className="bg-dark text-light" <Dropdown.Item
onClick={() => this.join(r.namespace)} key={r.namespace}
> className="bg-dark text-light"
{r.namespace} onClick={() => this.join(r.namespace)}
</Dropdown.Item> >
))} {r.namespace}
</Dropdown.Item>
))}
</Dropdown.Menu> </Dropdown.Menu>
</Dropdown> </Dropdown>
</Nav> </Nav>