Subversion Repository Public Repository

Nextrek

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const express = require('express');
const app = express();
const session = require('express-session');
const http = require('http').Server(app);
const io = require('socket.io')(http);
const crypto = require('crypto');
const ChatSingleton = require('./chat');

const Chat = ChatSingleton.instance;


let connect_sid = Date.now();

app.use(session({
    secret: 'keyboard cat',
    resave: true,
    saveUninitialized: true,
    cookie: { secure: false }
}));

app.get('/', function (req, res) {
    console.log(new Date().getTime() + ":[get: /]");
    
    //connect_sid = req.sessionID;
    console.log('connect_sid', connect_sid);
    
    res.sendFile(__dirname + '/public/index.html');
});

app.get('/customer', (req, res) => {
    console.log(new Date().getTime() + ":[get: /customer]");
    
    //connect_sid = req.sessionID;
    console.log('connect_sid', connect_sid);
    
    res.sendFile(__dirname + '/public/customer.html');
});

app.get('/operator', (req, res) => {
    console.log(new Date().getTime() + ":[get: /]");
    
    //connect_sid = req.sessionID;
    console.log('connect_sid', connect_sid);
    
    res.sendFile(__dirname + '/public/operator.html');
});

app.use(express.static('public'));

http.listen(3001, function () {
    console.log('listening on *:3001');
});


io.on('connection', (socket) => {
    
    const EVENTS = Chat.EVENTS;
    const ERRORS = Chat.ERRORS;
    const ROLES = Chat.ROLES;
    const DEFAULT_ROOMS = Chat.DEFAULT_ROOMS;
    let event = '';
    let user = undefined;
    
    
    // create user and send updates
    socket.on(EVENTS.CONNECTION.OPEN, (data) => {
        console.log('CONNECTION:OPEN', JSON.stringify(data));
        
        // create new user
        const buf = crypto.randomBytes(32);
        const uniqID = buf.toString('hex');
        user = Chat.createUser(data.name, data.role, uniqID, socket);

        console.log('user', user);

        // join user own room
        socket.join(user.room);

        // notify user is ready
        event = Chat.event(EVENTS.ROOM.CREATED, Chat.getUser(user.room));
        io.to(user.room).emit(event.type, event.data);

        // join operators room if role is operator
        if (user.role === ROLES.OPERATOR) {
            socket.join(DEFAULT_ROOMS.OPERATORS);
        }

        // send customers updated list to operators
        event = Chat.event(EVENTS.CUSTOMERS.LIST.UPDATE, Chat.getUsersByRole(ROLES.CUSTOMER));
        io.to(DEFAULT_ROOMS.OPERATORS).emit(event.type, event.data);

        // send operators updated list to operators
        event = Chat.event(EVENTS.OPERATORS.LIST.UPDATE, Chat.getUsersByRole(ROLES.OPERATOR));
        io.to(DEFAULT_ROOMS.OPERATORS).emit(event.type, event.data);

        // send rooms updated list to operators
        event = Chat.event(EVENTS.ROOMS.LIST.UPDATE, Chat.rooms);
        io.to(DEFAULT_ROOMS.OPERATORS).emit(event.type, event.data);
    });


    // operator joins customer's room
    socket.on(EVENTS.ROOM.JOIN, (data) => {
        if (!user) {
            return
        }
        
        const hostRoom = Chat.joinRoom(data.user, user);

        socket.join(hostRoom);
        
        event = Chat.event(EVENTS.ROOM.JOIN, user);
        io.to(hostRoom).emit(event.type, event.data);

        console.log('join room', user.name, event.data);
    });


    // send message
    socket.on(EVENTS.SEND.MESSAGE, (data) => {
        if (!user) {
            return
        }
        
        const room = user.role === ROLES.CUSTOMER ? user.room : data.room;

        Chat.message(data, room, user)
        .then(message => {
            event = Chat.event(EVENTS.SEND.MESSAGE, message);
            io.to(room).emit(event.type, event.data);
    
            console.log('sendMessage', event);
        });
    });


    // leave the room
    socket.on(EVENTS.ROOM.LEAVE, (data) => {
        if (!user) {
            return
        }

        const host = data && data.user || user;
        const promises = Chat.leaveRoom(host, user);

        Promise.all(promises).then(guestSockets => {            
            socket.leave(host.room);

            event = Chat.event(EVENTS.ROOM.LEAVE, user);
            io.to(host.room).emit(event.type, event.data);

            guestSockets.forEach(guestSocket => {
                guestSocket.leave(host.room);
            });
        });
    });


    socket.on('disconnect', () => {
        console.log('disconnect', user);
        
        if (!user) {
            return
        }

        event = Chat.event(EVENTS.CONNECTION.CLOSE);
        io.to(user.room).emit(event.type, event.data);
        
        const promises = Chat.deleteUser(user);
        
        Promise.all(promises).then(guestSockets => {            
            event = Chat.event(EVENTS.ROOM.LEAVE, user);
            io.to(user.room).emit(event.type, event.data);
            
            guestSockets.forEach(guestSocket => {
                guestSocket.leave(user.room);
            });
        });

        const userRole = user.role;

        // send operators updated list to operators
        if (userRole === ROLES.OPERATOR) {
            event = Chat.event(EVENTS.OPERATORS.LIST.UPDATE, Chat.getUsersByRole(ROLES.OPERATOR));
            io.to(DEFAULT_ROOMS.OPERATORS).emit(event.type, event.data);
        }

        // send customers updated list to operators
        if (userRole === ROLES.CUSTOMER) {
            event = Chat.event(EVENTS.CUSTOMERS.LIST.UPDATE, Chat.getUsersByRole(ROLES.CUSTOMER));
            io.to(DEFAULT_ROOMS.OPERATORS).emit(event.type, event.data);
        }
    });
});

Commits for Nextrek/socketIO/index.js

Diff revisions: vs.
Revision Author Commited Message
1129 Diff Diff GGentile picture GGentile Wed 23 Jan, 2019 23:23:52 +0000

room leave and notify guests

1128 Diff Diff GGentile picture GGentile Fri 18 Jan, 2019 09:29:09 +0000

room leave

1127 Diff Diff GGentile picture GGentile Wed 09 Jan, 2019 13:47:30 +0000

send image

1126 Diff Diff GGentile picture GGentile Fri 04 Jan, 2019 14:09:04 +0000
1125 Diff Diff GGentile picture GGentile Fri 04 Jan, 2019 11:31:52 +0000

event ROOM:CREATED

1124 Diff Diff GGentile picture GGentile Fri 04 Jan, 2019 11:20:58 +0000
1123 Diff Diff GGentile picture GGentile Fri 04 Jan, 2019 00:19:22 +0000
1122 Diff Diff GGentile picture GGentile Wed 02 Jan, 2019 13:31:08 +0000
1121 Diff Diff GGentile picture GGentile Wed 02 Jan, 2019 13:10:03 +0000

chat singleton

1120 GGentile picture GGentile Tue 04 Dec, 2018 10:31:37 +0000