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 |
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); console.log('user', user); // join user own room socket.join(user.room); // notify user is ready event = Chat.event(EVENTS.ROOM.CREATED, user); 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); }); // join room socket.on(EVENTS.ROOM.JOIN, (data) => { if (!user) { return } const ownerRoom = Chat.joinOwnerRoom(data.user, user); socket.join(ownerRoom); event = Chat.event(EVENTS.ROOM.JOIN, user); io.to(ownerRoom).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); }); }); socket.on('disconnect', () => { console.log('disconnect', user); event = Chat.event(EVENTS.CONNECTION.CLOSE); io.emit(event.type, event.data); if (!user) { return } const userRole = user.role; Chat.deleteUser(user); // 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
Revision | Author | Commited | Message |
---|---|---|---|
1127 Diff | GGentile | Wed 09 Jan, 2019 13:47:30 +0000 | send image |
1126 Diff | GGentile | Fri 04 Jan, 2019 14:09:04 +0000 | |
1125 Diff | GGentile | Fri 04 Jan, 2019 11:31:52 +0000 | event ROOM:CREATED |
1124 Diff | GGentile | Fri 04 Jan, 2019 11:20:58 +0000 | |
1123 Diff | GGentile | Fri 04 Jan, 2019 00:19:22 +0000 | |
1122 Diff | GGentile | Wed 02 Jan, 2019 13:31:08 +0000 | |
1121 Diff | GGentile | Wed 02 Jan, 2019 13:10:03 +0000 | chat singleton |
1120 Diff | GGentile | Tue 04 Dec, 2018 10:31:37 +0000 | |
1119 Diff | GGentile | Mon 03 Dec, 2018 23:13:28 +0000 | |
1116 | MOliva | Tue 25 Sep, 2018 14:16:19 +0000 |