// event for connection success ws.once('connect', (connection) => { debug('connection to translation endpoint succeeded'); this.connection = connection; this.connection.sendFile = sendFile; this.connection.sendStream = sendStream; // return the successful socket connection for use return callback(null, connection); });
const makeTest = protocols => test => { const server = jstp.ws.createServer([app]); test.tearDown(() => { server.close(); }); server.listen(0, () => { const client = new WebSocketClient(); const url = `ws://localhost:${server.address().port}`; client.once('connect', connection => { connection.close(); test.fail('unreachable code: WS protocol negotiation is probably broken'); test.end(); }); client.once('connectFailed', () => { test.end(); }); client.connect(url, protocols); }); }
// Create and connect WebSocketClient to get WebSocketConnection. // webSocketConfig - web socket client configuration, passed directly // to WebSocketClient constructor // options - will be destructured and passed directly to // WebSocketClient.connect. The last argument of options // is optional callback that will be called when connection // is established. // const connectionFactory = (webSocketConfig, ...options) => { const callback = common.extractCallback(options); const wsClient = new WebSocketClient(webSocketConfig); options[1] = constants.WEBSOCKET_PROTOCOL_NAME; wsClient.once('connect', connection => { callback(null, connection); }); wsClient.once('connectFailed', callback); wsClient.connect(...options); }
// event for connection failure ws.once('connectFailed', (error) => { debug('connection to translation endpoint failed:', error); return callback(error); });