-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·66 lines (53 loc) · 1.41 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
var app = require('express')(),
http = require('http').Server(app),
path = require('path'),
bodyParser = require('body-parser'),
io = require('socket.io')(http);
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.listen(process.env.PORT || 5000, function() {
console.log('Lets the chat begin!');
});
function User(socket) {
var self = this;
self.socket = socket;
}
User.prototype.joinChat = function(chat) {
var self = this;
self.chat = chat;
}
function Chat() {
var self = this;
self.users = [];
self.io = io;
self.addHandlers();
}
Chat.prototype.addHandlers = function() {
var self = this;
self.io.sockets.on("connection", function(socket) {
self.addUser(new User(socket));
// Adding handlers
socket.on('sendMessage', function(message) {
var currentDateTime = new Date().toLocaleString();
// io.emit("userTypingUpdate", typingUsers);
io.emit('newChatMessage', clientNickname, message, currentDateTime);
});
});
}
Chat.prototype.addUser = function(player) {
var self = this;
var user = "user" + (self.users.length + 1);
self.users.push(user);
console.log(user + "joined");
self.startChat();
}
Chat.prototype.startChat = function() {
// this.player1.socket.emit("startChat")
}
// Start the chat server
var chat = new Chat()