This commit is contained in:
notnull 2019-07-26 14:56:24 -04:00
parent a2e7396e56
commit 7cdd78e603
2 changed files with 91 additions and 52 deletions

View File

@ -1,6 +1,9 @@
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()
sendNamespaces() sendNamespaces()
@ -23,9 +26,14 @@ module.exports = io => {
}) })
}) })
socket.on('chat', (from, to) => io.to(to).emit('chat', from))
socket.on('part', () => { socket.on('part', () => {
console.log('part') console.log('part')
}) })
socket.on('change nick', (socketId, nick) => {
users[socketId]['nick'] = nick
})
socket.on('get namespaces', () => sendNamespaces()) socket.on('get namespaces', () => sendNamespaces())
@ -38,7 +46,10 @@ 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).map(socketId => users[socketId])
)
socket.on('disconnect', async () => { socket.on('disconnect', async () => {
io.emit('user disconnected', { socketId: socket.id }) io.emit('user disconnected', { socketId: socket.id })

View File

@ -9,7 +9,8 @@ const initialState = {
loading: true, loading: true,
messages: [], messages: [],
message: '', message: '',
namespaces: [], allNamespaces: [],
chats: [],
namespace: '/', namespace: '/',
allUsers: [], allUsers: [],
user: {}, user: {},
@ -33,9 +34,9 @@ class App extends React.Component {
componentDidMount() { componentDidMount() {
this.fetchData() this.fetchData()
this.socket.on('connect', () => { this.socket.on('connect', () => {
console.log('connected!') 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 }) this.setState({ user, namespace: this.socket.id })
}) })
this.socket.on('user connected', payload => { this.socket.on('user connected', payload => {
@ -46,9 +47,9 @@ class App extends React.Component {
console.log('a user disconnected.', payload) console.log('a user disconnected.', payload)
}) })
this.socket.on('got namespaces', namespaces => { this.socket.on('got all namespaces', allNamespaces => {
console.log('got namespaces:', namespaces) console.log('got all namespaces:', allNamespaces)
this.setState({ namespaces }) this.setState({ allNamespaces })
}) })
this.socket.on('got users', allUsers => { this.socket.on('got users', allUsers => {
@ -56,11 +57,25 @@ class App extends React.Component {
this.setState({ allUsers }) this.setState({ allUsers })
}) })
this.socket.on('nick change', (socketId, nick) => {
const allUsers = this.state.allUsers
allUsers.map(u => {
if (u.socketId === socketId) u.nick = nick
return u
})
this.setState({ allUsers })
})
this.socket.on('message', msg => { this.socket.on('message', msg => {
if (msg.namespace === this.state.namespace) msg.read = true if (msg.namespace === this.state.namespace) msg.read = true
const messages = this.state.messages.concat(msg) const messages = this.state.messages.concat(msg)
this.setState({ messages }) this.setState({ messages })
}) })
this.socket.on('chat', socketId => {
const chats = this.state.chats.concat(socketId)
this.setState({ chats })
})
} }
handleChange(e) { handleChange(e) {
@ -82,10 +97,8 @@ class App extends React.Component {
const message = { const message = {
id: uuid(), id: uuid(),
date: moment().format('MMMM D YYYY, h:mm a'), date: moment().format('MMMM D YYYY, h:mm a'),
socketId: this.socket.id,
text: this.state.message, text: this.state.message,
namespace: this.state.namespace, from: this.state.user.socketId,
from: this.socket.id,
to: this.state.namespace, to: this.state.namespace,
read: false, read: false,
} }
@ -103,10 +116,20 @@ class App extends React.Component {
join(namespace) { join(namespace) {
if (namespace.slice(0, 1) !== '#') return alert('Use a leading #, silly!') if (namespace.slice(0, 1) !== '#') return alert('Use a leading #, silly!')
console.log('joining', namespace) console.log('joining', namespace)
const chats = this.state.chats.concat(namespace)
this.socket.emit('join', namespace) this.socket.emit('join', namespace)
this.setState({ namespace }) this.setState({ namespace, chats })
}
query(user) {
console.log(`starting query with ${user.socketId}`)
const chats = this.state.chats.concat(user.socketId)
this.setState({ namespace: user.socketId, chats })
} }
sendMessage(msg) { sendMessage(msg) {
if (msg.to[0] !== '#' && !this.state.messages.find(m => m.to === msg.to)) {
console.log(`openign a chat with ${msg.to}`)
this.socket.emit('chat', msg.to)
}
this.socket.emit('message', msg) this.socket.emit('message', msg)
} }
@ -148,11 +171,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.socketId !== this.socket.id)
{u} .map(u => (
</Dropdown.Item> <Dropdown.Item
))} key={u.socketId}
className="bg-dark text-light"
onClick={() => this.query(u)}
>
{u.socketId}
</Dropdown.Item>
))}
</Dropdown.Menu> </Dropdown.Menu>
</Dropdown> </Dropdown>
<Dropdown> <Dropdown>
@ -161,15 +190,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.allNamespaces
<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>
@ -192,35 +223,32 @@ class App extends React.Component {
<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.chats.map(c => (
.filter( <Button
r => key={c}
r.namespace[0] === '#' && className="btn btn-dark d-flex"
r.sockets.includes(this.state.user.socketId) onClick={() => this.activateChat(c)}
) >
.map(r => ( <div
<Button className={`mr-auto ${
key={r.namespace} this.findUnreadMessages(c) > 0 ? 'text-danger' : ''
className="btn btn-dark d-flex" }`}
onClick={() => {
this.readMessages(r.namespace)
this.setState({ namespace: r.namespace })
}}
> >
<div className="mr-auto">{r.namespace}</div> {c}
{/*<div className="mr-auto badge bg-success"> </div>
{/*<div className="mr-auto badge bg-success">
this.state.messages.filter( this.state.messages.filter(
m => m.namespace === r.namespace m => m.namespace === r.namespace
).length ).length
</div> */} </div> */}
{this.findUnreadMessages(r.namespace) > 0 ? ( {this.findUnreadMessages(c) > 0 ? (
<div className="mr-auto badge bg-danger"> <div className="mr-auto badge bg-danger">
{this.findUnreadMessages(r.namespace)} {this.findUnreadMessages(c)}
</div> </div>
) : null} ) : null}
<div className="ml-auto">{r.sockets.length}</div> <div className="ml-auto">count</div>
</Button> </Button>
))} ))}
</div> </div>
{/*Main Section*/} {/*Main Section*/}
@ -235,7 +263,7 @@ class App extends React.Component {
<div className="d-flex flex-column border border-secondary flex-grow-1"> <div className="d-flex flex-column border border-secondary flex-grow-1">
<div className="bg-dark flex-grow-1 p-2 text-light"> <div className="bg-dark flex-grow-1 p-2 text-light">
{this.state.messages {this.state.messages
.filter(m => m.namespace === this.state.namespace) .filter(m => m.to === this.state.namespace)
.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>
@ -257,7 +285,7 @@ class App extends React.Component {
type="submit" type="submit"
onSubmit={this.handleSubmit} onSubmit={this.handleSubmit}
> >
submit submit as {this.state.user.nick}
</Button> </Button>
</form> </form>
</div> </div>