-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add message channel to packager for sending commands to bridge (2/N)
Reviewed By: frantic Differential Revision: D2957599 fb-gh-sync-id: 0d3f39bb5757c7af8ee6d178f4839af81928b8de shipit-source-id: 0d3f39bb5757c7af8ee6d178f4839af81928b8de
- Loading branch information
1 parent
7eb7b94
commit c377f2a
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
'use strict'; | ||
|
||
|
||
function attachToServer(server, path) { | ||
var WebSocketServer = require('ws').Server; | ||
var wss = new WebSocketServer({ | ||
server: server, | ||
path: path | ||
}); | ||
var interfaceSocket, shellSocket; | ||
|
||
function send(dest, message) { | ||
if (!dest) { | ||
return; | ||
} | ||
|
||
try { | ||
dest.send(message); | ||
} catch(e) { | ||
console.warn(e); | ||
// Sometimes this call throws 'not opened' | ||
} | ||
} | ||
|
||
wss.on('connection', function(ws) { | ||
const {url} = ws.upgradeReq; | ||
|
||
if (url.indexOf('role=interface') > -1) { | ||
if (interfaceSocket) { | ||
ws.close(1011, 'Another debugger is already connected'); | ||
return; | ||
} | ||
interfaceSocket = ws; | ||
interfaceSocket.onerror = | ||
interfaceSocket.onclose = () => { | ||
interfaceSocket = null; | ||
// if (shellSocket) { | ||
// shellSocket.close(1011, 'Interface was disconnected'); | ||
// } | ||
}; | ||
|
||
interfaceSocket.onmessage = ({data}) => { | ||
send(shellSocket, data) | ||
}; | ||
} else if (url.indexOf('role=shell') > -1) { | ||
if (shellSocket) { | ||
shellSocket.onerror = shellSocket.onclose = shellSocket.onmessage = null; | ||
shellSocket.close(1011, 'Another client connected'); | ||
} | ||
shellSocket = ws; | ||
shellSocket.onerror = | ||
shellSocket.onclose = () => { | ||
shellSocket = null; | ||
send(interfaceSocket, JSON.stringify({method: '$disconnected'})); | ||
}; | ||
shellSocket.onmessage = ({data}) => send(interfaceSocket, data); | ||
|
||
// console.log('CLIENT ----'); | ||
// if (doIt) { | ||
// console.log('> sending: %s', str); | ||
// send(shellSocket, str); | ||
// console.log('< sending'); | ||
// } | ||
|
||
} else { | ||
ws.close(1011, 'Missing role param'); | ||
} | ||
}); | ||
|
||
return { | ||
server: wss, | ||
isChromeConnected: function() { | ||
return !!interfaceSocket; | ||
} | ||
}; | ||
} | ||
|
||
module.exports = { | ||
attachToServer: attachToServer | ||
}; |
c377f2a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bottledwalter would you mind summarizing what this does? This looks very interesting, is there an issue you could refer to?
It looks like it allows the packager to let the application know about certain events?