-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·116 lines (92 loc) · 3.16 KB
/
index.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
107
108
109
110
111
112
113
114
115
// Setup basic express server
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var port = process.env.PORT || 3000;
server.listen(port, function () {
console.log('Server listening at port %d', port);
});
app.use(express.static(__dirname + '/public'));
var sentences = [
'Accessibility is not a barrier to innovation.',
'Don’t use color as the only visual means of conveying information.',
'Ensure sufficient contrast between text and its background.',
'Provide visual focus indication for keyboard focus.',
'Be careful with forms.',
'Don’t make people hover to find things.',
'Provide informative, unique page titles',
'Use headings to convey meaning and structure',
'Make link text meaningful',
'Keep content clear and concise',
'Write alternative text that provides the information or function of the image',
'Choose a content management system that supports accessibility',
'Use headings correctly to organize the structure of your content',
'Design your forms for accessibility',
'Use tables for tabular data, not for layout',
'Ensure that all content can be accessed with the keyboard alone in a logical way',
'Use ARIA roles and landmarks',
'Make dynamic content accessible',
'Screen reader users can use heading structure to navigate content.'
],
//the number of sentences in the array
maxSentences = sentences.length;
//get and return a random sentences from array
function getRandomSentence() {
//calculate a random index
var index = Math.floor(Math.random() * (maxSentences - 1));
//return the random sentence
return sentences[index];
}
// Chatroom
var botInterval;
var numUsers = 0;
var connections = 0;
io.on('connection', function (socket) {
if(connections === 0) {
connections++;
botInterval = setInterval(function sendBotMessage() {
var botMessage = getRandomSentence();
socket.broadcast.emit('new message', {
username: 'Chat bot',
message: botMessage
});
}, 10000);
}
var addedUser = false;
// when the client emits 'new message', this listens and executes
socket.on('new message', function (data) {
// we tell the client to execute 'new message'
socket.broadcast.emit('new message', {
username: socket.username,
message: data
});
});
// when the client emits 'add user', this listens and executes
socket.on('add user', function (username) {
if (addedUser) return;
// we store the username in the socket session for this client
socket.username = username;
++numUsers;
addedUser = true;
socket.emit('login', {
numUsers: numUsers
});
// echo globally (all clients) that a person has connected
socket.broadcast.emit('user joined', {
username: socket.username,
numUsers: numUsers
});
});
// when the user disconnects.. perform this
socket.on('disconnect', function () {
if (addedUser) {
--numUsers;
// echo globally that this client has left
socket.broadcast.emit('user left', {
username: socket.username,
numUsers: numUsers
});
}
});
});