diff --git a/server/socket/index.js b/server/socket/index.js index ff42423..49f30f1 100644 --- a/server/socket/index.js +++ b/server/socket/index.js @@ -1,6 +1,9 @@ +const users = {} + module.exports = io => { io.on('connection', async socket => { 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', () => { sendUsers() sendNamespaces() @@ -23,9 +26,14 @@ module.exports = io => { }) }) + socket.on('chat', (from, to) => io.to(to).emit('chat', from)) + socket.on('part', () => { console.log('part') }) + socket.on('change nick', (socketId, nick) => { + users[socketId]['nick'] = nick + }) socket.on('get namespaces', () => sendNamespaces()) @@ -38,7 +46,10 @@ module.exports = io => { } 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 () => { io.emit('user disconnected', { socketId: socket.id }) diff --git a/src/App.js b/src/App.js index e4d4bc0..491b0fd 100644 --- a/src/App.js +++ b/src/App.js @@ -9,7 +9,8 @@ const initialState = { loading: true, messages: [], message: '', - namespaces: [], + allNamespaces: [], + chats: [], namespace: '/', allUsers: [], user: {}, @@ -33,9 +34,9 @@ class App extends React.Component { componentDidMount() { this.fetchData() this.socket.on('connect', () => { - console.log('connected!') - const user = { socketId: this.socket.id } - this.setState({ user }) + console.log(`I am ${socket.id}`) + const user = { socketId: this.socket.id, nick: this.socket.id } + this.setState({ user, namespace: this.socket.id }) }) this.socket.on('user connected', payload => { @@ -46,9 +47,9 @@ class App extends React.Component { console.log('a user disconnected.', payload) }) - this.socket.on('got namespaces', namespaces => { - console.log('got namespaces:', namespaces) - this.setState({ namespaces }) + this.socket.on('got all namespaces', allNamespaces => { + console.log('got all namespaces:', allNamespaces) + this.setState({ allNamespaces }) }) this.socket.on('got users', allUsers => { @@ -56,11 +57,25 @@ class App extends React.Component { 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 => { if (msg.namespace === this.state.namespace) msg.read = true const messages = this.state.messages.concat(msg) this.setState({ messages }) }) + + this.socket.on('chat', socketId => { + const chats = this.state.chats.concat(socketId) + this.setState({ chats }) + }) } handleChange(e) { @@ -82,10 +97,8 @@ class App extends React.Component { const message = { id: uuid(), date: moment().format('MMMM D YYYY, h:mm a'), - socketId: this.socket.id, text: this.state.message, - namespace: this.state.namespace, - from: this.socket.id, + from: this.state.user.socketId, to: this.state.namespace, read: false, } @@ -103,10 +116,20 @@ class App extends React.Component { join(namespace) { if (namespace.slice(0, 1) !== '#') return alert('Use a leading #, silly!') console.log('joining', namespace) + const chats = this.state.chats.concat(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) { + 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) } @@ -148,11 +171,17 @@ class App extends React.Component { - {this.state.allUsers.map(u => ( - - {u} - - ))} + {this.state.allUsers + .filter(u => u.socketId !== this.socket.id) + .map(u => ( + this.query(u)} + > + {u.socketId} + + ))} @@ -161,15 +190,17 @@ class App extends React.Component { - {this.state.namespaces.map(r => ( - this.join(r.namespace)} - > - {r.namespace} - - ))} + {this.state.allNamespaces + .filter(n => n.namespace[0] === '#') + .map(r => ( + this.join(r.namespace)} + > + {r.namespace} + + ))} @@ -192,35 +223,32 @@ class App extends React.Component {
server
{/*
{this.state.allUsers.length}
*/} - {this.state.namespaces - .filter( - r => - r.namespace[0] === '#' && - r.sockets.includes(this.state.user.socketId) - ) - .map(r => ( - - ))} + {this.findUnreadMessages(c) > 0 ? ( +
+ {this.findUnreadMessages(c)} +
+ ) : null} +
count
+ + ))} {/*Main Section*/} @@ -235,7 +263,7 @@ class App extends React.Component {
{this.state.messages - .filter(m => m.namespace === this.state.namespace) + .filter(m => m.to === this.state.namespace) .map(m => (
{m.date}
@@ -257,7 +285,7 @@ class App extends React.Component { type="submit" onSubmit={this.handleSubmit} > - submit + submit as {this.state.user.nick}