Last active
July 3, 2018 06:29
-
-
Save OliverLeitner/8f5e3aea0fc805d37ebce6a2fd82dfc7 to your computer and use it in GitHub Desktop.
webworker simple ping
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
{ | |
"author": "Jalal.Hejazi", | |
"info": "Response header info", | |
"Content-Type":"text/json", | |
"X-Powered-By":"nodejs", | |
"Access-Control-Allow-Origin":"*", | |
"next":"REST The WEB" | |
} |
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
var http = require('http'); | |
var fs = require('fs'); | |
var port = '1111' ; | |
http.createServer(function(request, response) { | |
response.writeHead(200, { | |
'Content-Type': 'application/json', | |
'Access-Control-Allow-Origin': '*', | |
'X-Powered-By':'nodejs' | |
}); | |
fs.readFile('data.json', function(err, content){ | |
response.write(content); | |
response.end(); | |
}); | |
}).listen(port); | |
console.log('Listening on port ' + port ); |
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
<!DOCTYPE html> | |
<html lang="de"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>webworkers tryaround</title> | |
<style> | |
div { clear:both; float:left; }; | |
</style> | |
</head> | |
<body> | |
<div id="ping"> | |
<button name="submit" onClick="javascript:doPing();">Ping</button> | |
</div> | |
<div id="pong"> </div> | |
<script src="trya.js"></script> | |
</body> | |
</html> |
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
doPing = (() => { | |
//call the webworker | |
var worker = new Worker('working.js'); | |
// Setup an event listener that will handle messages received from the worker. | |
worker.addEventListener('message', function(e) { | |
// Log the workers message. | |
console.log(e.data); | |
}, false); | |
worker.postMessage('Hello World'); | |
}); | |
doRead = (() => { | |
fetch('http://localhost:1111').then(res => { | |
if (res.ok) { | |
return res.json(); | |
} | |
}).then(json => { | |
console.log(json); | |
}); | |
}); |
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
// Setup an event listener that will handle messages sent to the worker. | |
self.addEventListener('message', e => { | |
// Send the message back. | |
//self.postMessage('You said: ' + e.data); | |
let url = 'http://localhost:1111'; | |
fetch(url).then(res => { | |
if (res.ok) { | |
return res.json(); | |
} else { | |
throw new Error('error with server'); | |
} | |
}).then(json => { | |
self.postMessage(json); | |
}).catch(err => { | |
self.postMessage(err.message); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment