add part and nick commands, store users as objects

This commit is contained in:
data 2019-07-26 20:14:46 +01:00
parent 100d45afdc
commit 0630f346b5
2 changed files with 60 additions and 22 deletions

View File

@ -1,8 +1,12 @@
const users = {}
module.exports = io => { module.exports = io => {
io.on('connection', async socket => { io.on('connection', async socket => {
console.log(`A socket connection to the server has been made: ${socket.id}`) console.log(`A socket connection to the server has been made: ${socket.id}`)
users[socket.id] = { socketId: socket.id, nick: socket.id }
socket.join('#projex', () => { socket.join('#projex', () => {
sendUsers() sendUsers() // back to socket
sendUser(users[socket.id]) // announced to all
sendNamespaces() sendNamespaces()
}) })
@ -17,23 +21,30 @@ module.exports = io => {
sendNamespaces() sendNamespaces()
}) })
}) })
socket.on('chat', id => { socket.on('chat', user => {
const target = io.sockets.sockets[id] const target = io.sockets.sockets[user.socketId]
if (!target || socket.id === target.id) { if (!target) return
console.log('Not starting chat.', socket, target) if (socket.id === target.id) {
console.log('Not allowing chat with itself', socket.id, target.id)
return return
} }
console.log(`${socket.id} wants to chat with ${target.id}`)
// create a shared namespace // create a shared namespace
const namespace = `${socket.id.substr(0, 5)}.${target.id.substr(0, 5)}` const namespace = `${socket.id.substr(0, 5)}.${target.id.substr(0, 5)}`
console.log(`${socket.id} starts chat with ${target.id} in ${namespace}`)
socket.join(namespace) socket.join(namespace)
target.join(namespace) target.join(namespace)
sendNamespaces() sendNamespaces()
}) })
socket.on('part', () => { socket.on('part', namespace => {
console.log('part') console.log('part', socket.id, namespace)
socket.leave(namespace, () => {
sendNamespaces()
})
})
socket.on('change nick', nick => {
io.emit('nick changed', { socketId: socket.id, nick: nick })
}) })
socket.on('get namespaces', () => sendNamespaces()) socket.on('get namespaces', () => sendNamespaces())
@ -46,14 +57,14 @@ module.exports = io => {
io.emit('got namespaces', namespaces) io.emit('got namespaces', namespaces)
} }
const sendUser = user => socket.broadcast.emit('user connected', user)
const sendUsers = () => const sendUsers = () =>
io.emit('got users', Object.keys(io.sockets.sockets)) socket.emit('got users', Object.keys(users).map(u => users[u]))
socket.on('disconnect', async () => { socket.on('disconnect', async () => {
io.emit('user disconnected', { socketId: socket.id }) io.emit('user disconnected', { socketId: socket.id })
console.log(`${socket.id} has disconnected.`) console.log(`${socket.id} has disconnected.`)
sendNamespaces() sendNamespaces()
sendUsers()
}) })
}) })
} }

View File

@ -34,16 +34,30 @@ class App extends React.Component {
this.fetchData() this.fetchData()
this.socket.on('connect', () => { this.socket.on('connect', () => {
console.log(`I am ${socket.id}`) console.log(`I am ${socket.id}`)
const user = { socketId: this.socket.id } const user = { socketId: this.socket.id, nick: this.socket.id }
this.setState({ user, nick: this.socket.id, namespace: this.socket.id }) this.setState({ user, namespace: this.socket.id })
}) })
this.socket.on('user connected', payload => { this.socket.on('user connected', user => {
console.log('a new user connected!', payload) console.log('a new user connected!', user.nick)
const allUsers = this.state.allUsers.concat(user)
this.setState({ allUsers })
}) })
this.socket.on('user disconnected', payload => { this.socket.on('nick changed', user => {
console.log('a user disconnected.', payload) const allUsers = this.state.allUsers
allUsers.map(u => {
if (u.socketId === user.socketId) u.nick = user.nick
return u
})
console.log('new nick', user)
this.setState({ allUsers })
})
this.socket.on('user disconnected', socketId => {
console.log('a user disconnected.', socketId)
const oldUsers = this.state.allUsers
const allUsers = oldUsers.filter(u => u.socketId !== socketId)
this.setState({ allUsers })
}) })
this.socket.on('got namespaces', namespaces => { this.socket.on('got namespaces', namespaces => {
@ -61,6 +75,10 @@ class App extends React.Component {
const messages = this.state.messages.concat(msg) const messages = this.state.messages.concat(msg)
this.setState({ messages }) this.setState({ messages })
}) })
this.socket.on('nick change', user => {
console.log('new nick', user)
})
} }
handleChange(e) { handleChange(e) {
@ -72,7 +90,8 @@ class App extends React.Component {
const cmd = args[0].slice(1) const cmd = args[0].slice(1)
console.log('command:', cmd) console.log('command:', cmd)
if (cmd === 'join') this.join(args[1]) if (cmd === 'join') this.join(args[1])
else if (cmd === 'nick') this.setState({ nick: args[1] }) else if (cmd === 'part') this.part(args[1])
else if (cmd === 'nick') this.nick(args[1])
this.setState({ message: '' }) this.setState({ message: '' })
} }
handleSubmit(e) { handleSubmit(e) {
@ -107,10 +126,18 @@ class App extends React.Component {
this.socket.emit('join', namespace) this.socket.emit('join', namespace)
this.setState({ namespace }) this.setState({ namespace })
} }
part(namespace) {
console.log(`leaving ${namespace}`)
socket.emit('part', namespace)
}
nick(nick) {
console.log(`Changing nick to ${nick}`)
this.socket.emit('change nick', nick)
}
query(user) { query(user) {
console.log(`starting query with ${user}`) console.log(`starting query with ${user.nick}`)
this.socket.emit('chat', user) this.socket.emit('chat', user)
this.setState({ namespace: user }) this.setState({ namespace: user.socketId })
} }
sendMessage(msg) { sendMessage(msg) {
this.socket.emit('message', msg) this.socket.emit('message', msg)
@ -159,14 +186,14 @@ class App extends React.Component {
<Dropdown.Menu className="bg-dark"> <Dropdown.Menu className="bg-dark">
{this.state.allUsers {this.state.allUsers
.filter(u => u !== socket.id) .filter(u => u.nick !== this.state.user.nick)
.map(u => ( .map(u => (
<Dropdown.Item <Dropdown.Item
key={u} key={u.socketId}
className="bg-dark text-light" className="bg-dark text-light"
onClick={() => this.query(u)} onClick={() => this.query(u)}
> >
{u} {u.nick}
</Dropdown.Item> </Dropdown.Item>
))} ))}
</Dropdown.Menu> </Dropdown.Menu>