Recently we migrated a Chrome extension to Microsoft Edge. For Edge hasn't implemented native messaging, so we want to communicate with native app by websocket via Edge extension background page.
After testing, we found that, in the background page websocket can access external host successfully, but localhost, even though access '127.0.0.1' failed. And we try to access localhost in the web page, it did!
Edge browser info: userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64; ServiceUI 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393" We have checked "Allow localhost loopback (this might put your device at risk)" from about:flags.
Does Edge extension background page support access localhost? If it does, how can we achieve it? If not, could anyone help?
We run the WebSocket server as this example: https://blog.idrsolutions.com/2013/12/websockets-an-introduction/.
The extension can be downloaded from: https://github.com/chhxia/Edge-Extension. The code of edge extension background js:
var ws;
function openSocket(){
var socket, path;
// path = 'wss://echo.websocket.org'; // successfully access this path.
path = 'ws://localhost:8080/EchoChamber/echo';
console.log( '===> Tested path :: ', path );
try {
ws = new WebSocket( path );
}
catch ( e ) {
console.error( '===> WebSocket creation error :: ', e );
}
ws.onopen = function(){
alert('open...');
ws.send('text');
}
ws.onmessage = function(e){
alert("receive: " + e.data);
}
ws.onclose = function(e){
ws = undefined;
alert('close...' + e);
}
}
(function(){
openSocket();
browser.browserAction.onClicked.addListener(function(tab) {
if(ws === undefined){
openSocket();
}else if(ws && ws.readyState === WebSocket.OPEN){
alert('send');
ws.send('text');
}else{
alert('websocket is closed.');
}
});
})();