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 b894753..e542c72 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 => {
@@ -165,10 +164,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)
@@ -213,15 +208,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
@@ -236,13 +230,13 @@ class App extends React.Component {
renderError() {
return
something went wrong.
}
- join(namespace) {
- if (namespace.slice(0, 1) !== '#') return alert('Use a leading #, silly!')
- if (this.state.chats.find(c => c === namespace)) return
- console.log('joining', namespace)
- const chats = this.state.chats.concat(namespace)
- this.socket.emit('join', namespace)
- this.setState({ namespace, chats })
+ join(room) {
+ if (room[0] !== '#') return alert('Use a leading #, silly!')
+ if (this.state.chats.find(c => c === room)) return
+ console.log('joining', room)
+ const chats = this.state.chats.concat(room)
+ this.socket.emit('join', room, ack => console.log('ack', ack))
+ this.setState({ room, chats })
}
part(namespace) {
console.log(`leaving ${namespace}`)
@@ -309,159 +303,21 @@ class App extends React.Component {
: this.state.namespace === this.state.user.socketId
? 'Namespace'
: 'Chat with'
-
+ console.log(this.socket)
return (
- {/*Navbar*/}
-
-
-
-
-
-
-
+
- {/*Side Bar 2*/}
-
-
- {/*Main Section*/}
-
- {/*Main Section Header*/}
-
-
- {title} {this.state.namespace}
-
-
- {/*Main Section Content*/}
-
-
- {this.state.messages
- .filter(
- m =>
- (m.to[0] === '#' && m.to === this.state.namespace) ||
- (m.to[0] !== '#' && m.from === this.state.namespace) ||
- (m.from === this.state.user.socketId &&
- m.to === this.state.namespace)
- )
- .map(m => {
- const rowColor = m.from === 'server' ? 'text-secondary' : ''
- return (
-
-
{m.date}
- {m.from === 'server' ? null : (
-
{m.nick}:
- )}
-
{m.text}
-
- )
- })}
-
-
-
-
+
+
)
diff --git a/src/components/MessageEntryForm.js b/src/components/MessageEntryForm.js
new file mode 100644
index 0000000..af420cf
--- /dev/null
+++ b/src/components/MessageEntryForm.js
@@ -0,0 +1,42 @@
+import React from 'react'
+import { Button } from 'react-bootstrap'
+
+class MessageEntryForm extends React.Component {
+ constructor() {
+ super()
+ this.state = { message: '' }
+ this.handleChange = this.handleChange.bind(this)
+ this.handleSubmit = this.handleSubmit.bind(this)
+ }
+
+ handleChange(e) {
+ this.setState({ [e.target.name]: e.target.value })
+ }
+
+ handleSubmit(e) {
+ e.preventDefault()
+ this.props.submitMessage(this.state.message)
+ }
+ render() {
+ return (
+
+ )
+ }
+}
+
+export default MessageEntryForm
diff --git a/src/components/Messages.js b/src/components/Messages.js
new file mode 100644
index 0000000..03f08d1
--- /dev/null
+++ b/src/components/Messages.js
@@ -0,0 +1,37 @@
+import React from 'react'
+import MessageEntryForm from './MessageEntryForm'
+const Messages = props => {
+ const { title, namespace, messages, user, submitMessage } = props
+ return (
+
+ {/*Main Section Header*/}
+
+
+ {title} {namespace}
+
+
+ {/*Main Section Content*/}
+
+
+ {messages
+ .filter(
+ m =>
+ (m.to[0] === '#' && m.to === namespace) ||
+ (m.to[0] !== '#' && m.from === namespace) ||
+ (m.from === user.socketId && m.to === namespace)
+ )
+ .map(m => (
+
+
{m.date}
+
{m.nick}
+
{m.text}
+
+ ))}
+
+
+
+
+ )
+}
+
+export default Messages
diff --git a/src/components/Sidebar.js b/src/components/Sidebar.js
new file mode 100644
index 0000000..3f5bf32
--- /dev/null
+++ b/src/components/Sidebar.js
@@ -0,0 +1,60 @@
+import React from 'react'
+import { Button } from 'react-bootstrap'
+
+const Sidebar = props => {
+ const {
+ activateChat,
+ chats,
+ allUsers,
+ countUnreadMessages,
+ allNamespaces,
+ } = props
+ return (
+
+ )
+}
+
+export default Sidebar
diff --git a/src/components/Topbar.js b/src/components/Topbar.js
new file mode 100644
index 0000000..7600b54
--- /dev/null
+++ b/src/components/Topbar.js
@@ -0,0 +1,56 @@
+import React from 'react'
+import { Navbar, Nav, Dropdown } from 'react-bootstrap'
+
+const Topbar = props => {
+ const { allUsers, user, allNamespaces, query, join } = props
+
+ return (
+
+
+
+
+
+
+ )
+}
+
+export default Topbar
diff --git a/src/components/index.js b/src/components/index.js
new file mode 100644
index 0000000..83c2698
--- /dev/null
+++ b/src/components/index.js
@@ -0,0 +1,3 @@
+export { default as Messages } from './Messages'
+export { default as Sidebar } from './Sidebar'
+export { default as Topbar } from './Topbar'