* Handle a new open connection.
(ws: WebSocket)
| 51 | * Handle a new open connection. |
| 52 | */ |
| 53 | onOpen(ws: WebSocket): any { |
| 54 | if (this.server.options.debug) { |
| 55 | Log.websocketTitle('👨🔬 New connection:'); |
| 56 | Log.websocket({ ws }); |
| 57 | } |
| 58 | |
| 59 | ws.sendJson = (data) => { |
| 60 | try { |
| 61 | ws.send(JSON.stringify(data)); |
| 62 | |
| 63 | this.updateTimeout(ws); |
| 64 | |
| 65 | if (ws.app) { |
| 66 | this.server.metricsManager.markWsMessageSent(ws.app.id, data); |
| 67 | } |
| 68 | |
| 69 | if (this.server.options.debug) { |
| 70 | Log.websocketTitle('✈ Sent message to client:'); |
| 71 | Log.websocket({ ws, data }); |
| 72 | } |
| 73 | } catch (e) { |
| 74 | // |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | ws.id = this.generateSocketId(); |
| 79 | ws.subscribedChannels = new Set(); |
| 80 | ws.presence = new Map<string, PresenceMemberInfo>(); |
| 81 | |
| 82 | if (this.server.closing) { |
| 83 | ws.sendJson({ |
| 84 | event: 'pusher:error', |
| 85 | data: { |
| 86 | code: 4200, |
| 87 | message: 'Server is closing. Please reconnect shortly.', |
| 88 | }, |
| 89 | }); |
| 90 | |
| 91 | return ws.end(4200); |
| 92 | } |
| 93 | |
| 94 | this.checkForValidApp(ws).then(validApp => { |
| 95 | if (!validApp) { |
| 96 | ws.sendJson({ |
| 97 | event: 'pusher:error', |
| 98 | data: { |
| 99 | code: 4001, |
| 100 | message: `App key ${ws.appKey} does not exist.`, |
| 101 | }, |
| 102 | }); |
| 103 | |
| 104 | return ws.end(4001); |
| 105 | } |
| 106 | |
| 107 | ws.app = validApp.forWebSocket(); |
| 108 | |
| 109 | this.checkIfAppIsEnabled(ws).then(enabled => { |
| 110 | if (!enabled) { |
no test coverage detected