-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslack.js
More file actions
86 lines (78 loc) · 3.29 KB
/
slack.js
File metadata and controls
86 lines (78 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const express = require('express');
const app = express();
const socketio = require('socket.io')
let namespaces = require('./data/namespaces');
app.use(express.static(__dirname + '/public'));
const expressServer = app.listen(9000);
const io = socketio(expressServer);
// io.on = io.of('/').on = io.sockets.on
// io.emit = io.of('/').emit = io.sockets.emit
io.on('connection',(socket)=>{
// console.log(socket.handshake)
// build an array to send back with the img and endpoing for each NS
let nsData = namespaces.map((ns)=>{
return {
img: ns.img,
endpoint: ns.endpoint
}
})
// console.log(nsData)
socket.emit('nsList',nsData);
})
// loop through each namespace and listen for a connection
namespaces.forEach((namespace)=>{
// console.log(namespace)
// const thisNs = io.of(namespace.endpoint)
io.of(namespace.endpoint).on('connection',(nsSocket)=>{
console.log(nsSocket.handshake)
const username = nsSocket.handshake.query.username;
// console.log(`${nsSocket.id} has join ${namespace.endpoint}`)
// a socket has connected to one of our chatgroup namespaces.
// send that ns gorup info back
nsSocket.emit('nsRoomLoad',namespace.rooms)
nsSocket.on('joinRoom',(roomToJoin,numberOfUsersCallback)=>{
// deal with history... once we have it
console.log(nsSocket.rooms);
const roomToLeave = Object.keys(nsSocket.rooms)[1];
nsSocket.leave(roomToLeave);
updateUsersInRoom(namespace, roomToLeave)
nsSocket.join(roomToJoin)
// io.of('/wiki').in(roomToJoin).clients((error, clients)=>{
// console.log(clients.length)
// numberOfUsersCallback(clients.length);
// })
const nsRoom = namespace.rooms.find((room)=>{
return room.roomTitle === roomToJoin;
})
nsSocket.emit('historyCatchUp', nsRoom.history)
updateUsersInRoom(namespace, roomToJoin);
})
nsSocket.on('newMessageToServer',(msg)=>{
const fullMsg = {
text: msg.text,
time: Date.now(),
username: username,
avatar: 'https://via.placeholder.com/30'
}
// console.log(fullMsg)
// Send this message to ALL the sockets that are in the room that THIS socket is in.
// console.log(nsSocket.rooms)
// get the keys
const roomTitle = Object.keys(nsSocket.rooms)[1];
const nsRoom = namespace.rooms.find((room)=>{
return room.roomTitle === roomTitle;
})
// console.log("The room object that we made that matches this NS room is...")
// console.log(nsRoom)
nsRoom.addMessage(fullMsg);
io.of(namespace.endpoint).to(roomTitle).emit('messageToClients',fullMsg)
})
})
})
function updateUsersInRoom(namespace, roomToJoin){
// Send back the number of users in this room to ALL sockets connected to this room
io.of(namespace.endpoint).in(roomToJoin).clients((error,clients)=>{
// console.log(`There are ${clients.length} in this room`);
io.of(namespace.endpoint).in(roomToJoin).emit('updateMembers',clients.length)
})
}