Skip to content

Instantly share code, notes, and snippets.

@rpragana
Last active June 19, 2017 00:12
Show Gist options
  • Save rpragana/b7c440fc9b3abdd882e64a595dbda75d to your computer and use it in GitHub Desktop.
Save rpragana/b7c440fc9b3abdd882e64a595dbda75d to your computer and use it in GitHub Desktop.
Web Workers
<!doctype html>
<html>
<head> <meta charset="utf-8"> </head>
<body>
<a href="javascript:sendMsg()">send msg</a>
<p id="log"></p>
<script>
var worker = new Worker('task.js');
worker.addEventListener('message', function(e) {
document.getElementById('log').innerHTML +=
'O web worker respondeu: '+e.data+'<br />';
}, false);
var n=1;
function sendMsg() {
worker.postMessage(n++ +': testando...');
}
worker.postMessage('Olá, Web Worker!');
</script>
</body>
</html>
#!/usr/bin/env nodemon
var express = require('express');
var app = express()
app.use(express.static(__dirname + '/'));
app.listen(8000);
console.log('Aponte seu navegador para http://localhost:8000/');
self.addEventListener('message', function(e) {
self.postMessage("Recebi: "+e.data);
}, false);
var worker = new Worker('task.js');
worker.addEventListener('message', function(e) {
console.log('Worker said: ', e.data);
}, false);
var n=1;
function sendMsg() {
worker.postMessage(n++ +': testando...');
}
worker.postMessage('Hello World'); // Send data to our worker.

Web Workers

  • index.html
  • webworkers.js
  • task.js
  • smserver.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment