-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
106 lines (89 loc) · 2.46 KB
/
client.js
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
'use strict'
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const program = require('commander')
const spacebroClient = require('spacebro-client')
let hostValue
let portValue
let username
let channel
let maxHistory = process.stdout.rows
let chatHistory = []
if (!Date.now) {
Date.now = function () { return new Date().getTime(); }
}
parseArgs()
spacebroClient.connect(hostValue, portValue, {
clientName: username,
channelName: channel,
verbose: false
})
spacebroClient.once('new-member', startChat)
function parseArgs () {
program.version('0.0.14')
.usage('[options] <host> <port>')
.arguments('<host> <port>')
.action(function (host, port) {
hostValue = host
portValue = port
})
.option('-u, --user [name]', 'Set username')
.option('-c, --channel [name]', 'Join channel')
program.parse(process.argv)
if (!hostValue || !portValue) {
program.outputHelp()
console.log(!hostValue ? '\nNo host given.' : 'No port given.')
process.exit()
}
username = program.user || 'Visitor'
channel = program.channel || 'random'
}
function startChat (data) {
displayMessages()
addEvents()
spacebroClient.emit('user-connected', { username: username })
}
function addEvents () {
spacebroClient.on('user-connected', function (data) {
addNewMessage({
timestamp: getTime(),
user: '',
message: `${data.username} has connected !`
})
displayMessages()
})
rl.on('line', (input) => {
let time = getTime()
spacebroClient.emit('new-message', {user: username, message: input, timestamp: time})
})
spacebroClient.on('new-message', (data) => {
addNewMessage(data)
displayMessages()
})
}
function getTime () {
let date = new Date()
let hours = (date.getHours() < 10 ? '0' : '') + date.getHours().toString()
let minutes = (date.getMinutes() < 10 ? '0' : '') + date.getMinutes().toString()
let seconds = (date.getSeconds() < 10 ? '0' : '') + date.getSeconds().toString()
let datenow = hours + ':' + minutes + ':' + seconds
return datenow
}
function addNewMessage (data) {
let newMessage = `[${data.timestamp}] ${data.user}: ${data.message}`
if (chatHistory.length >= maxHistory) {
chatHistory.shift()
chatHistory.push(newMessage)
} else {
chatHistory.push(newMessage)
}
}
function displayMessages () {
console.log('\x1Bc')
chatHistory.forEach((message) => {
console.log(message)
})
}