| 
									
										
										
										
											2017-11-25 17:43:05 -05:00
										 |  |  | const WebSocket = require('ws'); | 
					
						
							| 
									
										
										
										
											2017-11-30 23:50:42 -05:00
										 |  |  | const utils = require('./utils'); | 
					
						
							|  |  |  | const log = require('./log'); | 
					
						
							| 
									
										
										
										
											2017-11-25 17:43:05 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | let webSocketServer; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-30 23:50:42 -05:00
										 |  |  | function init(httpServer, sessionParser) { | 
					
						
							|  |  |  |     webSocketServer = new WebSocket.Server({ | 
					
						
							|  |  |  |         verifyClient: (info, done) => { | 
					
						
							|  |  |  |             sessionParser(info.req, {}, () => { | 
					
						
							|  |  |  |                 const allowed = utils.isElectron() || info.req.session.loggedIn; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (!allowed) { | 
					
						
							|  |  |  |                     log.error("WebSocket connection not allowed because session is neither electron nor logged in."); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 done(allowed) | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         server: httpServer | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-25 17:43:05 -05:00
										 |  |  |     webSocketServer.on('connection', function connection(ws, req) { | 
					
						
							|  |  |  |         console.log("websocket client connected"); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 18:33:23 -05:00
										 |  |  | async function sendMessage(message) { | 
					
						
							| 
									
										
										
										
											2017-11-25 17:43:05 -05:00
										 |  |  |     const jsonStr = JSON.stringify(message); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     webSocketServer.clients.forEach(function each(client) { | 
					
						
							|  |  |  |         if (client.readyState === WebSocket.OPEN) { | 
					
						
							|  |  |  |             client.send(jsonStr); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |     init, | 
					
						
							| 
									
										
										
										
											2017-11-28 18:33:23 -05:00
										 |  |  |     sendMessage | 
					
						
							| 
									
										
										
										
											2017-11-25 17:43:05 -05:00
										 |  |  | }; |