webworker.js 892 B

12345678910111213141516171819202122232425262728
  1. importScripts ("/stomp.js");
  2. // *WebWorker* onmessage implementation
  3. onmessage = function (event) {
  4. var url = event.data.url;
  5. var login = event.data.login;
  6. var passcode = event.data.passcode;
  7. var destination = event.data.destination;
  8. var text = event.data.text;
  9. // create the Stomp client
  10. var client = Stomp.client(url);
  11. // connect to the server
  12. client.connect(login, passcode, function(frame) {
  13. // upon connection, subscribe to the destination
  14. var sub = client.subscribe(destination, function(message) {
  15. // when a message is received, post it to the current WebWorker
  16. postMessage("WebWorker: " + message.body);
  17. //... unsubscribe from the destination
  18. sub.unsubscribe();
  19. //... and disconnect from the server
  20. client.disconnect();
  21. });
  22. // send the text to the destination
  23. client.send(destination, {}, text);
  24. });
  25. };