Chromium Code Reviews| Index: chrome/test/data/subresource_filter/websocket_connection.js |
| diff --git a/chrome/test/data/subresource_filter/websocket_connection.js b/chrome/test/data/subresource_filter/websocket_connection.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4d5db4cb67fb48281f0c2b8abd3eb0bae616ba03 |
| --- /dev/null |
| +++ b/chrome/test/data/subresource_filter/websocket_connection.js |
| @@ -0,0 +1,32 @@ |
| +// Note: This JS can run either in a worker or as a script in the main document |
| +// context. |
| +function connectWebSocket(url, inWorker) { |
| + function messageMainContext(data) { |
| + if (inWorker) |
| + self.postMessage(data); |
| + else |
| + self.postMessage(data, location); |
| + } |
| + var ws = new WebSocket(url); |
| + |
| + ws.addEventListener('open', function() { |
|
pkalinnikov
2017/03/02 12:33:53
Is there a reason why you use such a syntax instea
Charlie Harrison
2017/03/02 14:48:00
My understanding is the addEventListener pattern i
yhirano
2017/03/03 06:15:22
Either is fine, I think.
Personally, I prefer add
pkalinnikov
2017/03/03 10:19:06
On the second look, I find plenty of "ws.onopen" u
Charlie Harrison
2017/03/03 13:59:26
Yes. Indeed you are right onopen seems more popula
|
| + ws.send('hello world'); |
| + }); |
| + |
| + ws.addEventListener('close', function() { |
| + messageMainContext("onclose"); |
|
pkalinnikov
2017/03/02 12:33:53
nit: Please use single quotes here as well.
Charlie Harrison
2017/03/02 14:48:00
Done.
|
| + }); |
| + |
| + ws.addEventListener('message', function() { |
| + messageMainContext('onmessage'); |
| + }); |
| +} |
| + |
| +self.addEventListener('message', function(e) { |
| + // Will only receive a message with a URL in it if this JS is executing in a |
| + // worker. |
| + if (e.data.url) { |
| + connectWebSocket(e.data.url, true /* inWorker */); |
| + } |
| +}); |
| + |