1
0
forked from notnull/ircz

add /nick command

This commit is contained in:
data 2019-07-26 13:14:48 +01:00
parent 9fd8ac7969
commit 100d45afdc
2 changed files with 14 additions and 15 deletions

View File

@ -11,11 +11,6 @@ module.exports = io => {
io.to(message.namespace).emit('message', message) io.to(message.namespace).emit('message', message)
}) })
socket.on('get users', () => {
console.log('get users')
sendUsers()
})
socket.on('join', namespace => { socket.on('join', namespace => {
console.log('join:', namespace) console.log('join:', namespace)
socket.join(namespace, () => { socket.join(namespace, () => {
@ -52,7 +47,7 @@ module.exports = io => {
} }
const sendUsers = () => const sendUsers = () =>
socket.emit('got users', Object.keys(io.sockets.sockets)) io.emit('got users', Object.keys(io.sockets.sockets))
socket.on('disconnect', async () => { socket.on('disconnect', async () => {
io.emit('user disconnected', { socketId: socket.id }) io.emit('user disconnected', { socketId: socket.id })

View File

@ -35,7 +35,7 @@ class App extends React.Component {
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 }
this.setState({ user }) this.setState({ user, nick: this.socket.id, namespace: this.socket.id })
}) })
this.socket.on('user connected', payload => { this.socket.on('user connected', payload => {
@ -72,6 +72,7 @@ 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] })
this.setState({ message: '' }) this.setState({ message: '' })
} }
handleSubmit(e) { handleSubmit(e) {
@ -85,7 +86,7 @@ class App extends React.Component {
socketId: this.socket.id, socketId: this.socket.id,
text: this.state.message, text: this.state.message,
namespace: this.state.namespace, namespace: this.state.namespace,
from: this.socket.id, from: this.state.nick,
to: this.state.namespace, to: this.state.namespace,
read: false, read: false,
} }
@ -140,8 +141,8 @@ class App extends React.Component {
const title = const title =
this.state.namespace[0] === '#' this.state.namespace[0] === '#'
? 'Channel' ? 'Channel'
: this.state.namespace[0] === '/' : this.state.namespace === this.state.user.socketId
? 'server messages' ? 'Me'
: 'User' : 'User'
return ( return (
@ -201,16 +202,19 @@ class App extends React.Component {
> >
<h5 className=" border-bottom pb-2">Chats</h5> <h5 className=" border-bottom pb-2">Chats</h5>
<Button <Button
key="/" key={this.state.user.socketId}
className="btn btn-dark d-flex" className="btn btn-dark d-flex"
onClick={() => this.activateChat('/')} onClick={() => this.activateChat(this.state.user.socketId)}
> >
<div className="mr-auto">server</div> <div className="mr-auto">server</div>
{/*<div className="ml-auto">{this.state.allUsers.length}</div>*/} {/*<div className="ml-auto">{this.state.allUsers.length}</div>*/}
</Button> </Button>
{this.state.namespaces {this.state.namespaces
.filter( .filter(
r => r.sockets && r.sockets.includes(this.state.user.socketId) r =>
r.sockets &&
r.sockets.includes(this.state.user.socketId) &&
r.namespace !== this.state.user.socketId
) )
.map(r => ( .map(r => (
<Button <Button
@ -258,7 +262,7 @@ class App extends React.Component {
.map(m => ( .map(m => (
<div className="d-flex justify-content-between" key={m.id}> <div className="d-flex justify-content-between" key={m.id}>
<div>{m.date}</div> <div>{m.date}</div>
<div>{m.socketId}</div> <div>{m.from}</div>
<div>{m.text}</div> <div>{m.text}</div>
</div> </div>
))} ))}
@ -276,7 +280,7 @@ class App extends React.Component {
type="submit" type="submit"
onSubmit={this.handleSubmit} onSubmit={this.handleSubmit}
> >
submit submit as {this.state.nick}
</Button> </Button>
</form> </form>
</div> </div>