|
| 1 | +var http = require('http').createServer(handler) |
| 2 | + , config = require('./settings'); |
| 3 | + |
| 4 | + |
| 5 | +const redis = require('redis'); |
| 6 | +const redisClient = redis.createClient(config.redis.port,config.redis.host,config.redis.options); |
| 7 | + |
| 8 | +var io = require(config.socketiopath).listen(http); |
| 9 | + |
| 10 | +// enable all transports (optional if you want flashsocket support, please note that some hosting |
| 11 | +// providers do not allow you to create servers that listen on a port different than 80 or their |
| 12 | +// default port) |
| 13 | +io.set('transports',[ |
| 14 | + 'websocket', |
| 15 | + 'polling', |
| 16 | + 'xhr-polling', |
| 17 | + 'jsonp-polling', |
| 18 | + 'polling']); |
| 19 | + |
| 20 | +http.listen(config.web.port,config.web.host); |
| 21 | +console.log('LHC Server listening on '+config.web.host+':'+config.web.port); |
| 22 | + |
| 23 | +function handler(req, res) { |
| 24 | + res.writeHead(200); |
| 25 | + res.end(); |
| 26 | +} |
| 27 | + |
| 28 | +console.log("Debug enabled - "+config.debug.output); |
| 29 | + |
| 30 | +io.sockets.on('connection', function (socket) { |
| 31 | + var redisClient = null; |
| 32 | + |
| 33 | + try { |
| 34 | + if (config.use_publish_notifications == true && config.ignore_desktop_client == false) { |
| 35 | + redisClient = redis.createClient(config.redis.port,config.redis.host,config.redis.options); |
| 36 | + |
| 37 | + redisClient.on("message", function(channel, message) { |
| 38 | + if (channel.indexOf('admin_room_') !== -1) { |
| 39 | + socket.emit('syncbackoffice',message); |
| 40 | + } else { |
| 41 | + socket.emit('syncforce', message); |
| 42 | + } |
| 43 | + }); |
| 44 | + } |
| 45 | + } catch (e) { |
| 46 | + if (config.debug.output == true) { |
| 47 | + throw e; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + socket.on('newmessage', function (data) { |
| 52 | + try { |
| 53 | + if (config.debug.output == true) { |
| 54 | + console.log('newmessage:'+data.instance_id+'_'+data.chat_id); |
| 55 | + }; |
| 56 | + |
| 57 | + if (data.data.message_id != "0") { |
| 58 | + socket.broadcast.to('chat_room_'+data.instance_id+'_'+data.chat_id).emit('newmessage', data); |
| 59 | + } |
| 60 | + } catch (e) { |
| 61 | + if (config.debug.output == true) { |
| 62 | + throw e; |
| 63 | + } |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + socket.on('userpostedmessage', function (data) { |
| 68 | + try { |
| 69 | + if (config.debug.output == true) { |
| 70 | + console.log('userpostedmessage:'+data.instance_id+'_'+data.chat_id); |
| 71 | + }; |
| 72 | + socket.broadcast.to('chat_room_'+data.instance_id+'_'+data.chat_id).emit('userpostedmessage', data); |
| 73 | + } catch (e) { |
| 74 | + if (config.debug.output == true) { |
| 75 | + throw e; |
| 76 | + } |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + socket.on('userstartedpostmessage', function (data) { |
| 81 | + try { |
| 82 | + if (config.debug.output == true) { |
| 83 | + console.log('userstartedpostmessage:'+data.instance_id+'_'+data.chat_id); |
| 84 | + }; |
| 85 | + socket.broadcast.to('chat_room_'+data.instance_id+'_'+data.chat_id).emit('userstartedpostmessage', data); |
| 86 | + } catch (e) { |
| 87 | + if (config.debug.output == true) { |
| 88 | + throw e; |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + socket.on('usertyping', function (data) { |
| 94 | + try { |
| 95 | + if (config.debug.output == true) { |
| 96 | + console.log('usertyping:'+data.instance_id+'_'+data.chat_data.chat_id+'-'+data.chat_data.status); |
| 97 | + }; |
| 98 | + socket.broadcast.to('chat_room_'+data.instance_id+'_'+data.chat_data.chat_id).emit('usertyping', data.chat_data); |
| 99 | + } catch (e) { |
| 100 | + if (config.debug.output == true) { |
| 101 | + throw e; |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + socket.on('operatortyping', function (data) { |
| 107 | + try { |
| 108 | + if (config.debug.output == true) { |
| 109 | + console.log('operatortyping:'+data.instance_id+'_'+data.chat_data.chat_id+'-'+data.chat_data.status); |
| 110 | + }; |
| 111 | + socket.broadcast.to('chat_room_'+data.instance_id+'_'+data.chat_data.chat_id).emit('operatortyping', data.chat_data); |
| 112 | + } catch (e) { |
| 113 | + if (config.debug.output == true) { |
| 114 | + throw e; |
| 115 | + } |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + socket.on('userleftchat', function (data) { |
| 120 | + try { |
| 121 | + if (config.debug.output == true) { |
| 122 | + console.log('userleftchat:'+data.instance_id+'_'+data.chat_id); |
| 123 | + }; |
| 124 | + socket.broadcast.to('chat_room_'+data.instance_id+'_'+data.chat_id).emit('userleftchat', data.chat_id); |
| 125 | + } catch (e) { |
| 126 | + if (config.debug.output == true) { |
| 127 | + throw e; |
| 128 | + } |
| 129 | + } |
| 130 | + }); |
| 131 | + |
| 132 | + socket.on('join', function (data) { |
| 133 | + try { |
| 134 | + if (config.debug.output == true) { |
| 135 | + console.log('join:'+data.instance_id+'_'+data.chat_id); |
| 136 | + }; |
| 137 | + socket.join('chat_room_'+data.instance_id+'_'+data.chat_id); |
| 138 | + socket.broadcast.to('chat_room_'+data.instance_id+'_'+data.chat_id).emit('userjoined', data.chat_id); |
| 139 | + |
| 140 | + if (config.use_publish_notifications == true && config.ignore_desktop_client == false) { |
| 141 | + if (config.debug.output == true) { |
| 142 | + console.log('subscribed_to_channel:'+'chat_room_' + data.instance_id + '_' + data.chat_id); |
| 143 | + } |
| 144 | + redisClient.subscribe('chat_room_' + data.instance_id + '_' + data.chat_id); |
| 145 | + } |
| 146 | + } catch (e) { |
| 147 | + if (config.debug.output == true) { |
| 148 | + throw e; |
| 149 | + } |
| 150 | + } |
| 151 | + }); |
| 152 | + |
| 153 | + socket.on('join_admin', function (data) { |
| 154 | + try { |
| 155 | + if (config.debug.output == true) { |
| 156 | + console.log('join_admin:'+data.instance_id); |
| 157 | + }; |
| 158 | + |
| 159 | + if (redis !== undefined) { |
| 160 | + if (config.use_publish_notifications == true && config.ignore_desktop_client == true) { |
| 161 | + redisClient = redis.createClient(config.redis.port,config.redis.host,config.redis.options); |
| 162 | + redisClient.on("message", function(channel, message) { |
| 163 | + socket.emit('syncbackoffice',message); |
| 164 | + }); |
| 165 | + }; |
| 166 | + redisClient.subscribe('admin_room_' + data.instance_id); |
| 167 | + } |
| 168 | + } catch (e) { |
| 169 | + if (config.debug.output == true) { |
| 170 | + throw e; |
| 171 | + } |
| 172 | + } |
| 173 | + }); |
| 174 | + |
| 175 | + socket.on('leave', function (data) { |
| 176 | + try { |
| 177 | + if (config.debug.output == true) { |
| 178 | + console.log('leave:'+data.instance_id+'_'+data.chat_id); |
| 179 | + }; |
| 180 | + socket.leave('chat_room_'+data.instance_id+'_'+data.chat_id); |
| 181 | + |
| 182 | + if (config.use_publish_notifications == true && config.ignore_desktop_client == false) { |
| 183 | + redisClient.unsubscribe('chat_room_' + data.instance_id + '_' + data.chat_id); |
| 184 | + } |
| 185 | + } catch (e) { |
| 186 | + if (config.debug.output == true) { |
| 187 | + throw e; |
| 188 | + } |
| 189 | + } |
| 190 | + }); |
| 191 | + |
| 192 | + socket.on('syncforce', function (data) { |
| 193 | + try { |
| 194 | + if (config.debug.output == true) { |
| 195 | + console.log('syncforce:'+data.instance_id+'_'+data.chat_id); |
| 196 | + }; |
| 197 | + socket.broadcast.to('chat_room_'+data.instance_id+'_'+data.chat_id).emit('syncforce', data.chat_id); |
| 198 | + } catch (e) { |
| 199 | + if (config.debug.output == true) { |
| 200 | + throw e; |
| 201 | + } |
| 202 | + } |
| 203 | + }); |
| 204 | + |
| 205 | + socket.on('disconnect', function() { |
| 206 | + try { |
| 207 | + if (redisClient !== null) { |
| 208 | + redisClient.quit(); |
| 209 | + } |
| 210 | + } catch (e) { |
| 211 | + if (config.debug.output == true) { |
| 212 | + throw e; |
| 213 | + } |
| 214 | + } |
| 215 | + }); |
| 216 | + |
| 217 | +}); |
0 commit comments