-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
110 lines (88 loc) · 3.24 KB
/
server.js
File metadata and controls
110 lines (88 loc) · 3.24 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const io = require('socket.io')();
const port = 8000;
io.listen(port);
console.log('Listening on port: ', port);
const messages = [];
let messageID = 0;
// this creates a message object we can store.
function getTimestamp() {
const date = new Date();
let hours = date.getHours();
let minutes = date.getMinutes();
const ampm = hours >= 12 ? 'pm' : 'am';
hours %= 12;
hours = hours || 12; // the hour '0' should be '12'
minutes = minutes < 10 ? `0${minutes}` : minutes;
const strTime = `${hours}:${minutes} ${ampm}`;
return strTime;
}
const createMessage = (client, message, name) => ({
client,
message,
messageID,
name,
timestamp: getTimestamp(),
});
// For testing large amounts of messages
// for (let i = 0; i < 2000; i++) {
// messages.push(createMessage('Server', {
// message: i,
// userName: 'Server',
// }, 'Server'));
// messageID += 1;
// }
io.on('connection', (client) => {
//console.log(client.handshake)
var room = client.handshake['headers'].referer.split("/").pop();
client.join(room)
console.log('user joined room #'+room);
// clients.push(client);
client.on('sentMessage', (message) => {
console.log('received', message)
console.log(messages)
// this message is a packet, it's an object with two properties, a message, and a name
messageID += 1;
// add identifying information
// .log('Message received from user: ', message);
// we want to send to all of the clients
// console.log(createMessage(client, message));
// Objects that contain both room and messages, each specific to each other.
// Need to check for an object where room matches, then append to that objects messages.
// the same goes for the fetching of messages.
if (message.message !== '') {
const messageToSend = createMessage(client.id, message, message.userName);
if(messages[room] === undefined){
messages.push(room)
}
messages[room].push(messageToSend);
io.to(room).emit('message', messageToSend);
}
});
client.on('historyRequest', () => {
// The client is asking for the history of the chat
// messages.forEach((message) => {
// client.emit('message', message);
//
// // console.log('emitted message');
// });
client.emit('historyPacket', messages[room].slice(-40));
// client.emit('historyPacket', messages);
});
client.on('messagesRequest', (earliestMessageID) => {
// A client has requested more messages, we give them 40 at a time
if (earliestMessageID === 1) {
// We return an empty Array
client.emit('previousMessages', []);
} else {
client.emit('previousMessages', messages[room].slice((earliestMessageID - 40), (earliestMessageID)));
}
});
client.on('isTyping', (packet) => {
// Will eventually have to handle users closing the window before the update that they're not typing comes in.
// console.log(packet.userName, ' is typing? ', packet.isTyping);
// Ideally I'm making user obejcts and storing all of this locally at which point i can send on updates and broadcast.
// This will also fix the issue where users that disconnect before the update is sent will fail.
// console.log(packet);
client.broadcast.emit('userTypingUpdate', packet);
});
});