diff --git a/server/socket/index.js b/server/socket/index.js index 5dd4e98..129bb89 100644 --- a/server/socket/index.js +++ b/server/socket/index.js @@ -18,23 +18,27 @@ module.exports = io => { io.to(message.to).emit('message', message) }) - socket.on('join', namespace => { - const socketId = socket.id - console.log(`${socketId} joins ${namespace}`) - socket.join(namespace, () => { - io.to(namespace).emit('user joined', { namespace, socketId }) - sendNamespace(namespace) + socket.on('join', room => { + console.log(`${socket.id} joins ${room}`) + socket.join(room, () => { + io.to(room).emit('user joined', { room, socketId: socket.id }) + io.of(room).clients((error, clients) => { + if (error) throw error + socket.emit('got client list', clients) + }) }) }) - - socket.on('part', namespace => { - const socketId = socket.id - console.log('part', socketId, namespace) - socket.leave(namespace, () => { - io.to(namespace).emit('user left', { namespace, socketId }) - sendNamespace(namespace) + socket.on('get all clients', () => { + io.clients((e, c) => { + if (e) console.log(e) + console.log('all clients:', c) }) }) + socket.on('part', namespace => { + console.log('part', socket.id, namespace) + io.to(namespace).emit('user left', { namespace, socketId: socket.id }) + socket.leave(namespace) + }) socket.on('change nick', nick => { io.emit('changed nick', { socketId: socket.id, nick: nick }) @@ -56,14 +60,5 @@ module.exports = io => { console.log(`${socket.id} has disconnected.`) //sendNamespaces() }) - - const sendNamespace = namespace => { - if (!io.sockets.adapter.rooms[namespace]) return - const newNamespace = { - namespace, - sockets: Object.keys(io.sockets.adapter.rooms[namespace].sockets), - } - io.emit('got namespace', newNamespace) - } }) } diff --git a/src/App.js b/src/App.js index bdfd691..ab53f45 100644 --- a/src/App.js +++ b/src/App.js @@ -1,19 +1,16 @@ import React from 'react' import socket from './socket' +import { Topbar, Sidebar, Messages } from './components' -import { Navbar, Nav, Dropdown, Button } from 'react-bootstrap' import uuid from 'uuid' import moment from 'moment' const initialState = { loading: true, - messages: [], - message: '', - allNamespaces: [], - chats: [], - namespace: '/', - allUsers: [], - user: {}, + allSockets: [], + allMessages: [], + buffers: [], + buffer: '/', } class App extends React.Component { @@ -21,10 +18,13 @@ class App extends React.Component { super() this.state = initialState this.socket = socket - this.handleChange = this.handleChange.bind(this) this.submitMessage = this.submitMessage.bind(this) this.join = this.join.bind(this) this.readMessages = this.readMessages.bind(this) + this.countUnreadMessages = this.countUnreadMessages.bind(this) + this.readMessages = this.readMessages.bind(this) + this.activateChat = this.activateChat.bind(this) + this.query = this.query.bind(this) } async fetchData() { @@ -34,10 +34,9 @@ class App extends React.Component { componentDidMount() { this.fetchData() this.socket.on('connect', () => { - console.log(`I am ${socket.id}`) + console.log(`I am ${this.socket.id}`) this.join('#projex') - const user = { socketId: this.socket.id, nick: this.socket.id } - this.setState({ user }) + this.socket.nick = this.socket.id }) this.socket.on('user connected', user => { @@ -48,119 +47,122 @@ class App extends React.Component { this.socket.on('user disconnected', socketId => { console.log(`${socketId} disconnected.`) - const allUsers = this.state.allUsers.map(u => { - if (u.socketId === socketId) u.offline = true - return u - }) - const oldNick = allUsers.find(u => u.socketId === socketId).nick - this.setState({ allUsers }) - // inform channels user was part of - this.state.chats.map(c => { - if (c === socketId) { - const messages = this.state.messages.concat( - this.formatMessage( - `${oldNick} disconnected`, - socketId, - '[server]', - c, - c === this.state.namespace ? true : false - ) - ) - this.setState({ messages }) - } - this.state.allNamespaces.map(n => { - if (n.namespace === c && n.sockets.find(s => s === socketId)) { - const messages = this.state.messages.concat( - this.formatMessage( - `${oldNick} disconnected`, - socketId, - '[server]', - c, - c === this.state.namespace ? true : false - ) - ) - this.setState({ messages }) - } - return n - }) - return c - }) + // const allUsers = this.state.allUsers.map(u => { + // if (u.socketId === socketId) u.offline = true + // return u + // }) + // const oldNick = allUsers.find(u => u.socketId === socketId).nick + // this.setState({ allUsers }) + // // inform channels user was part of + // this.state.chats.map(c => { + // if (c === socketId) { + // const messages = this.state.messages.concat( + // this.formatMessage( + // `${oldNick} disconnected`, + // socketId, + // '[server]', + // c, + // c === this.state.namespace ? true : false + // ) + // ) + // this.setState({ messages }) + // } + // this.state.allNamespaces.map(n => { + // if (n.namespace === c && n.sockets.find(s => s === socketId)) { + // const messages = this.state.messages.concat( + // this.formatMessage( + // `${oldNick} disconnected`, + // socketId, + // '[server]', + // c, + // c === this.state.namespace ? true : false + // ) + // ) + // this.setState({ messages }) + // } + // return n + // }) + // return c + // }) }) this.socket.on('user joined', data => { - const { namespace, socketId } = data - const user = this.state.allUsers.find(u => u.socketId === socketId) - console.log(`${user.nick} joined ${namespace}`) - const messages = this.state.messages.concat( - this.formatMessage( - `${user.nick} joined ${namespace}`, - socketId, - '[server]', - namespace, - namespace === this.state.namespace ? true : false - ) - ) - this.setState({ messages }) + console.log(data) + // const { namespace, socketId } = data + // const user = this.state.allUsers.find(u => u.socketId === socketId) + // console.log(`${user.nick} joined ${namespace}`) + // const messages = this.state.messages.concat( + // this.formatMessage( + // `${user.nick} joined ${namespace}`, + // socketId, + // '[server]', + // namespace, + // namespace === this.state.namespace ? true : false + // ) + // ) + // this.setState({ messages }) }) this.socket.on('user left', data => { - const { namespace, socketId } = data - const user = this.state.allUsers.find(u => u.socketId === socketId) - console.log(`${user.nick} left ${namespace}`) - const messages = this.state.messages.concat( - this.formatMessage( - `${user.nick} left ${namespace}`, - socketId, - '[server]', - namespace, - namespace === this.state.namespace ? true : false - ) - ) - this.setState({ messages }) + console.log(data) + // const { namespace, socketId } = data + // const user = this.state.allUsers.find(u => u.socketId === socketId) + // console.log(`${user.nick} left ${namespace}`) + // const messages = this.state.messages.concat( + // this.formatMessage( + // `${user.nick} left ${namespace}`, + // socketId, + // '[server]', + // namespace, + // namespace === this.state.namespace ? true : false + // ) + // ) + // this.setState({ messages }) }) this.socket.on('changed nick', user => { - if (user.socketId === this.state.user.socketId) return - console.log(`${user.socketId} is now known as ${user.nick}.`) - const allUsers = this.state.allUsers - const oldNick = allUsers.find(u => u.socketId === user.socketId).nick - // inform channels user is part of - this.state.chats.map(c => { - if (c === user.socketId) { - const messages = this.state.messages.concat( - this.formatMessage( - `${oldNick} is now known as ${user.nick}`, - user.socketId, - '[server]', - c, - c === this.state.namespace ? true : false - ) - ) - this.setState({ messages }) - } - this.state.allNamespaces.map(n => { - if (n.namespace === c && n.sockets.find(s => s === user.socketId)) { - const messages = this.state.messages.concat( - this.formatMessage( - `${oldNick} is now known as ${user.nick}`, - user.socketId, - '[server]', - c, - c === this.state.namespace ? true : false - ) - ) - this.setState({ messages }) - } - return n - }) - return c - }) - // update nick - allUsers.map(u => { - if (u.socketId === user.socketId) u.nick = user.nick - return u - }) - this.setState({ allUsers }) + console.log(user) + // if (user.socketId === this.state.user.socketId) return + // console.log(`${user.socketId} is now known as ${user.nick}.`) + // const allUsers = this.state.allUsers + // const oldNick = allUsers.find(u => u.socketId === user.socketId).nick + // // inform channels user is part of + // this.state.chats.map(c => { + // if (c === user.socketId) { + // const messages = this.state.messages.concat( + // this.formatMessage( + // `${oldNick} is now known as ${user.nick}`, + // user.socketId, + // '[server]', + // c, + // c === this.state.namespace ? true : false + // ) + // ) + // this.setState({ messages }) + // } + // this.state.allNamespaces.map(n => { + // if (n.namespace === c && n.sockets.find(s => s === user.socketId)) { + // const messages = this.state.messages.concat( + // this.formatMessage( + // `${oldNick} is now known as ${user.nick}`, + // user.socketId, + // '[server]', + // c, + // c === this.state.namespace ? true : false + // ) + // ) + // this.setState({ messages }) + // } + // return n + // }) + // return c + // }) + // // update nick + // allUsers.map(u => { + // if (u.socketId === user.socketId) u.nick = user.nick + // return u + // }) + // this.setState({ allUsers }) }) this.socket.on('got namespace', namespace => { @@ -201,10 +203,6 @@ class App extends React.Component { }) } - handleChange(e) { - this.setState({ [e.target.name]: e.target.value }) - } - parseCommand() { const args = this.state.message.split(' ') const cmd = args[0].slice(1) @@ -239,15 +237,14 @@ class App extends React.Component { } return message } - submitMessage(e) { - e.preventDefault() - - if (this.state.message[0] === '/') return this.parseCommand() - if (!this.state.message.length > 0) return + submitMessage(msg) { + console.log(msg) + if (msg[0] === '/') return this.parseCommand() + if (!msg.length > 0) return this.sendMessage( this.formatMessage( - this.state.message, + msg, this.state.user.socketId, this.state.user.nick, this.state.namespace @@ -262,13 +259,13 @@ class App extends React.Component { renderError() { return