'WebSocket.URL' is deprecated. Please use 'WebSocket.url' instead - javascript

WebSocket connection to 'ws://localhost:35729/livereload' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED chromereload.js:9
'WebSocket.URL' is deprecated. Please use 'WebSocket.url' instead. chromereload.js:12
I'm getting this error message in my chrome extension since chrome updated to version 38. I'm not quite sure whats going on here, but now opening most things causes the extension to crash. I used yeoman to scaffold my project at the beginning, and everything was working fine. I tried removing livereload from the manifest, but that seems to break everything. Any help would be appreciated.
Here is the code:
'use strict';
// Reload client for Chrome Apps & Extensions.
// The reload client has a compatibility with livereload.
// WARNING: only supports reload command.
var LIVERELOAD_HOST = 'localhost:';
var LIVERELOAD_PORT = 35729;
var connection = new WebSocket('ws://' + LIVERELOAD_HOST + LIVERELOAD_PORT + '/livereload');
connection.onerror = function (error) {
console.log('reload connection got error' + JSON.stringify(error));
};
connection.onmessage = function (e) {
if (e.data) {
var data = JSON.parse(e.data);
if (data && data.command === 'reload') {
chrome.runtime.reload();
}
}
};

In Chrome this message is emitted when using something like JSON.stringify() or similar on a WebSocket object. These kind of routines will access the WebSocket.URL property, which is deprecated, and emit this warning. Even if your code doesn't explicitly call WebSocket.URL. You can delete this property to mute the warning from Chrome.
var ws = new WebSocket('wss://example.com/');
delete ws.URL;
console.log(JSON.stringify(ws));

Related

Chrome WebSocket connection closes immediately

I have been trying to setup a wss server using nodejs, and have encountered a problem when trying to connect to it using chrome. The problem still occurs with all extensions disabled and in an incognito window so I've ruled that out as the problem.
When trying to connect using chrome, I get the error:
WebSocket connection to 'wss://www.domain-name.com/' failed:
with no reason given. On the server, socket.on('close') is called immediately with description "Connection dropped by remote peer" The close event has wasClean = false. This error does not occur when connecting from safari and Firefox so I'm not really sure where to look to see what's causing it. It's running on AWS Lightsail, and through an Apache proxy server.
The client code:
var socket = new WebSocket("wss://www.domain-name.com", 'JSON')
socket.onopen = function (event) {
console.log('open');
socket.send('socket opened')};
socket.onclose = function (event) {
console.log(event)};
socket.onmessage = function(message) {
console.log('receiving message from server...')};
And the server code:
const WebSocketServer = require('websocket').server;
app = express()
var server = app.listen(3000, () => {
console.log('Server started');
});
app.use(express.static('public'));
var wsServer = new WebSocketServer({
httpServer: server
});
wsServer.on('request', function(request){
console.log('New connection');
var connection = request.accept(null, request.origin);
connection.send('welcome from server...');
connection.on('message', function(message){
console.log(message)};
connection.on('close', function(reasonCode, description) {
console.log('disconnecting', reasonCode, description);
});
});
I also got the same error before switching to a secure WebSocket server. Any help would be appreciated, I've run out of places to look and ways to try and get more information to help out what the problem is.
EDIT: it seems to work on chrome on my phone, but not on chrome on my friends phone?
The problem was not specifying the protocol when accepting the connection. After about 20 hours working on the same bug and implementing an SSL certificate to get it to work, I changed:
request.accept(null, request.origin);
to:
request.accept('json', request.origin);
For some reason the chrome gives a really unhelpful error message. Microsoft edge the same error occurs, but gives a much more helpful error message so I could work out what was going on.
In my case, this was caused by passing an unused options value as the third parameter to the WebSocket constructor. The options parameter is supported by Node.js's ws module but not by browsers; however, instead of displaying a clean error message, Chrome closed the connection without a good description.

TypeError: 'get binaryType' called on an object that does not implement interface WebSocket

Hello, some time ago, i started to make a userscript (tampermonkey script) where i get the active websocket, i have tried to get the websocket when the websocket send a message:
let ws;
window.WebSocket.prototype.send = function(){
if(!ws) {
ws = this
console.log(ws)
}
}
but not working, so i tried to handle messages but nothing showing up when a message was received:
let ws = window.WebSocket;
ws.onmessage = function(msg){
console.log(msg.data)
}
Finally i choosed to get websocket binaryType for reproduce it, but when i send
window.WebSocket.prototype.binaryType
in devtools, i got this error
(in latest Firefox)
TypeError: 'get binaryType' called on an object that does not implement interface WebSocket.
(in latest Microsoft Edge)
TypeError: Illegal Invocation
(i'm not good with English sorry, i'm French)
Thanks for answers
Thanks for answer, i have edited my script, code here:
let ws;
unsafeWindow.WebSocket.prototype.oldSend = unsafeWindow.WebSocket.prototype.send
unsafeWindow.WebSocket.prototype.send = function(m) {
if(!ws) {
ws = this
alert(ws)
}
unsafeWindow.WebSocket.prototype.oldSend.apply(this, [m])
}
//need that for get the websocket
unsafeWindow.WebSocket.prototype.send("ping")
This is working, i get the websocket but, i got an error:
TypeError: Illegal Invocation at WebSocket.unsafeWindow.WebSocket.prototype.send
what should i do?
[UPDATED!]
Line Error:
unsafeWindow.WebSocket.prototype.oldSend.apply(this, [m])
was called on the WebSocket object, not in the window.
Juste need to replace .apply to .bind
Corrected Line:
unsafeWindow.WebSocket.prototype.oldSend.bind(unsafeWindow, m)

Seeding via Webtorrent not working with webtorrent-hybrid on Node

Software versions:
"webtorrent-hybrid": "^4.0.1",
OS X 10.14.4
NPM: 6.13.4
Node: v8.17.0
var WebTorrent = require('webtorrent-hybrid')
var client = new WebTorrent()
client.on('error', (err) => {
console.error(`fatalError ${err.message || err}`);
process.exit(1);
});
client.seed('./c5l.mp4', function (torrent) {
torrent.on('warning', function (err) {
console.warn(err);
});
torrent.on('error', function (err) {
console.error(err);
});
console.log('client.seed done', {
magnetURI: torrent.magnetURI,
ready: torrent.ready,
paused: torrent.paused,
done: torrent.done,
infohash: torrent.infoHash
});
});
Gives me my magnet string. On the frontend I have:
script(src = 'https://cdnjs.cloudflare.com/ajax/libs/webtorrent/0.107.17/webtorrent.min.js')
script.
var client = new WebTorrent()
// working torrentId
var torrentId2 = 'magnet:?xt=urn:btih:c9e15763f722f23e98a29decdfae341b98d53056&dn=Cosmos+Laundromat&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fcosmos-laundromat.torrent'
// failing torrentId
var torrentId5 = 'magnet:?xt=urn:btih:f9435fb103d53a0b3cce14afbae867cc6999d76b&dn=c5l.mp4&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.openwebtorrent.com'
client.add(torrentId5, function (torrent) {
// Torrents can contain many files. Let's use the .mp4 file
var file = torrent.files.find(function (file) {
console.log(file);
return file.name.endsWith('.mp4')
})
// Display the file by adding it to the DOM.
// Supports video, audio, image files, and more!
file.appendTo('body')
})
When I have the working torrentId the video loads as expected. When I sub it out with the magnet url that was created via the webtorrent-hybrid package I first get some errors:
WebSocket connection to 'wss://tracker.fastcast.nz/' failed: Error in connection establishment: net::ERR_NAME_NOT_RESOLVED
Great, so I remove that tracker from the querystring, try again:
WebSocket connection to 'wss://tracker.btorrent.xyz/' failed: Unknown reason
Great, remove it again I get something that just hangs and nothing happens.
If I use the magnet on Brave it just hangs at Loading the torrent file list and the spinner endlessly spins.
What's going on? Thanks
// working torrentId
var torrentId2
magnet:?xt=urn:btih:c9e15763f722f23e98a29decdfae341b98d53056&dn=Cosmos+Laundromat&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fcosmos-laundromat.torrent
is/are a well seeded file(s) is means it works 90-100%
// failing torrentId
var torrentId5 = 'magnet:?xt=urn:btih:f9435fb103d53a0b3cce14afbae867cc6999d76b&dn=c5l.mp4&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.openwebtorrent.com'
is none or low seeded file(s) is means it works 0.1-10%
When I have the working torrentId the video loads as expected. When I
sub it out with the magnet url that was created via the
webtorrent-hybrid package I first get some errors:
WebSocket connection to 'wss://tracker.fastcast.nz/' failed: Error in
connection establishment: net::ERR_NAME_NOT_RESOLVED
Great, so I remove that tracker from the querystring, try again:
WebSocket connection to 'wss://tracker.btorrent.xyz/' failed: Unknown
reason
Great, remove it again I get something that just hangs and nothing
happens.
If I use the magnet on Brave it just hangs at Loading the torrent file
list and the spinner endlessly spins.
What's going on? Thanks
WebSocket connection to 'wss://tracker.fastcast.nz/' failed: Error in
connection establishment: net::ERR_NAME_NOT_RESOLVED
wss://tracker.fastcast.nz/ is dead and discontinued to support by the owner in year 2019
WebSocket connection to 'wss://tracker.btorrent.xyz/' failed: Unknown reason
is a busy tracker, the response rate is too long and runs in a cheap server due to low funding. so it works 30-90%. so the result is failed: Unknown reason
If I use the magnet on Brave it just hangs at Loading the torrent file
list and the spinner endlessly spins.
Webtorrent clients have some issues downloading/playing large files and unsupported file formats like .mkv (video/x-Matroska). And you can not see any notification if there is an error(s). It just hangs up.

A way to stop WebSocket errors to show up in browser's console

So the problem is when I try to initiate a new WebSocket to a remote host, sometimes the browser's console prints a red error message and complains about a refused connection, here is the message:
Error in connection establishment: net::ERR_CONNECTION_REFUSED
Having an error is fine since the remote host might be not responding sometimes, but the fact that I cannot handle this error message is very annoying.
Is there any way to either handle this error message or check whether the remote host accepts the WebSocket connection before initializing one in my JavaScript code??
Several possibilities come to mind:
Add a WebSocket.onerror error handler
myWebSocket.onerror = myEventHandler;
Wrap your "connect" in a try/catch block
try {
const connection = new WebSocket(myUrl);
...
}
catch(error) {
console.error(error);
}
Structure your code such that your I/O is event driven:
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#Examples
// Create WebSocket connection.
const socket = new WebSocket('ws://localhost:8080');
// Connection opened
socket.addEventListener('open', function (event) {
socket.send('Hello Server!');
});
// Listen for messages
socket.addEventListener('message', function (event) {
console.log('Message from server ', event.data);
});
// Handle errors
socket.addEventListener('error', function (event) {
console.log('WebSocket error observed:', event);
});
ADDENDUM:
The above methods allow you to completely handle a websockets exception.
Regardless of whether the exception is handled or not, the Chrome debugger will tell you if an exception has occurred. This is a Good Thing. It's called a "First-Chance Exception":
https://learn.microsoft.com/en-us/security-risk-detection/concepts/first-chance-exception
.. it is known a “first chance” exception – the debugger is given the
first chance of inspecting the exception prior to the application
handling it (or not).
In Microsoft's Visual Studio debugger, there's a little checkbox you can use to "gag" first chance exceptions. I'm not aware of any similar "checkbox" in Chrome debugger.
POSSIBLE SUGGESTIONS:
Chrome debugger has a "filter". EXAMPLE FILTER REGEX: ^((?!ERR_CONNECTION_REFUSED).)*$
This link suggests you might be able to use the filter to "Hide Network Messages" (I haven't tried it myself). See also this link.

shared web workers error handling

How to handle/throw errors in a shared web worker?
If you throw an error like this in a dedicated web worker the error appears in the console in the browser.. but not when the worker is shared..!?
With a dedicated web worker you can also use console.log() in the worker.. but not in a shared web worker
main page
var worker = new SharedWorker('js/webworker.js');
worker.port.onmessage = function(e){
console.log('From worker: '+e.data);
};
worker.port.onerror = function(e){
console.log(e.message+'\nLine: '+e.lineno+'\nFilename: '+e.filename);
};
worker.port.start();
worker.port.postMessage('This message should throw an error in the web worker');
shared web worker
var ports = [];
self.onconnect = function(e){
var port = e.ports[0];
ports.push(port);
port.onmessage = function(e){
port.postMessage(e.data);
throw Error('hehehe');
};
port.start();
port.postMessage('Worker connected!');
};
As the documentation says:
Whenever an uncaught runtime script error occurs in one of the worker's scripts, if the error did not occur while handling a previous script error, the user agent must report the error at the URL of the resource that contained the script, with the position (line number and column number) where the error occurred, in the origin of the scripts running in the worker, using the WorkerGlobalScope object's onerror attribute.
http://www.w3.org/TR/workers/
So the error will be reported to self and then sent to all the ports:
self.onerror = function (e) {
ports.forEach(function (port) { port.postMessage(e); });
};
Here is jsfiddle that demonstrates it: http://jsfiddle.net/nhrfgd1L/
(July 2021)
As an update to the already given answer, for Sharedworkers it is enough to write the respective event handler on the client side only (the web pages) as indicated here:
https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker/onerror
Basically, it is this function:
worker.onerror = function(error) {
console.log('Worker error: ' + error.message + '\n');
};
The error.message contains the details of the error if you want to display them or put them in a log.
The Sharedworker itself does not need to have any additional code to manage errors.

Categories

Resources