[Pkg-javascript-commits] [sockjs-client] 194/350: Fix #181 test inside of web worker
tonnerre at ancient-solutions.com
tonnerre at ancient-solutions.com
Fri Aug 5 01:04:20 UTC 2016
This is an automated email from the git hooks/post-receive script.
tonnerre-guest pushed a commit to branch upstream
in repository sockjs-client.
commit 8be97b2b0a7f7f6be661065526899a7d5268dc97
Author: Bryce Kahle <bkahle at gmail.com>
Date: Mon Oct 20 14:29:32 2014 -0400
Fix #181 test inside of web worker
---
Changelog | 2 +
tests/html/lib/worker.js | 36 +++++++++++++
tests/html/web-worker.html | 126 +++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 164 insertions(+)
diff --git a/Changelog b/Changelog
index 36d79b5..32de2b9 100644
--- a/Changelog
+++ b/Changelog
@@ -16,6 +16,8 @@
* Check for an empty response from /info request - #143
* Add Content-Type to XHR requests to fix issue over HTTPS on Galaxy S4 - #164
* Fix iframe fallback when message is sent from a popup in IE7/8 - #166
+ * Add support for query strings on the url - #74
+ * Now works inside of Web Workers - #181
0.3.4
diff --git a/tests/html/lib/worker.js b/tests/html/lib/worker.js
new file mode 100644
index 0000000..c9f4ab7
--- /dev/null
+++ b/tests/html/lib/worker.js
@@ -0,0 +1,36 @@
+/* global importScripts, postMessage, onmessage: true, SockJS */
+'use strict';
+importScripts('sockjs.js');
+
+var sjs;
+
+onmessage = function(e) {
+ var msg = JSON.parse(e.data);
+
+ if (msg.type === 'open') {
+ sjs = new SockJS(msg.url, null, msg.transports);
+ sjs.onmessage = function(e) {
+ postMessage(JSON.stringify({ type: 'message', data: e.data }));
+ };
+ sjs.onopen = function() {
+ postMessage(JSON.stringify({ type: 'open' }));
+ };
+ sjs.onclose = function(e) {
+ postMessage(JSON.stringify({ type: 'close', code: e.code, reason: e.reason }));
+ };
+ sjs.onerror = function(e) {
+ postMessage(JSON.stringify({ type: 'error ', data: e.toString() }));
+ };
+ return;
+ }
+
+ if (msg.type === 'message') {
+ sjs.send(msg.data);
+ return;
+ }
+
+ if (msg.type === 'close') {
+ sjs.close();
+ return;
+ }
+};
diff --git a/tests/html/web-worker.html b/tests/html/web-worker.html
new file mode 100644
index 0000000..44c933d
--- /dev/null
+++ b/tests/html/web-worker.html
@@ -0,0 +1,126 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />
+ <meta charset="UTF-8" />
+
+ <link href="data:image/x-icon;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQEAYAAABPYyMiAAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAAF0lEQVRIx2NgGAWjYBSMglEwCkbBSAcACBAAAeaR9cIAAAAASUVORK5CYII=" rel="icon" type="image/x-icon" />
+
+ <script type="text/javascript" src="lib/sockjs.js"></script>
+ <script type="text/javascript" src="static/jquery.min.js"></script>
+
+ <script type="text/javascript" src="config.js"></script>
+</head>
+<body>
+<form>
+ <select id="transport">
+ <option value="">- any - </option>
+ <option value="websocket">websocket</option>
+ <option value="xdr-streaming">xdr-streaming</option>
+ <option value="xhr-streaming">xhr-streaming</option>
+ <option value="eventsource">eventsource</option>
+ <option value="iframe-eventsource">iframe-eventsource</option>
+ <option value="htmlfile">htmlfile</option>
+ <option value="iframe-htmlfile">iframe-htmlfile</option>
+ <option value="xdr-polling">xdr-polling</option>
+ <option value="xhr-polling">xhr-polling</option>
+ <option value="iframe-xhr-polling">iframe-xhr-polling</option>
+ <option value="jsonp-polling">jsonp-polling</option>
+ </select>
+ <input type="checkbox" id="sameOrigin" checked>Same Origin?
+ <input type="button" value="Connect" id="connect">
+ <input type="button" value="Disconnect" id="disconnect" disabled="yes">
+</form>
+
+ Latency: <code id="latency"></code><br>
+ <code id="logs" style="height:200px; overflow:auto; display: block; border: 1px gray solid;">
+ </code>
+
+<script>
+ /* global $, clientOptions */
+ 'use strict';
+ function log(a) {
+ if ('console' in window && 'log' in window.console) {
+ console.log(a);
+ }
+ $('#logs').append($('<code>').text(a));
+ $('#logs').append($('<br>'));
+ $('#logs').scrollTop($('#logs').scrollTop() + 10000);
+ }
+
+ var worker;
+ function send() {
+ worker.postMessage(JSON.stringify({ type: 'message', data: JSON.stringify({ t: (new Date()).getTime()}) }));
+ }
+ function onopen() {
+ log('connected');
+ $('#sameOrigin').attr('disabled', true);
+ send();
+ }
+ function onclose(code, reason) {
+ log('disconnected ' + code + ', ' + reason);
+ $('#connect').each(function(_,e){
+ e.disabled = '';
+ });
+ $('#disconnect').attr('disabled', true);
+ $('#sameOrigin').attr('disabled', false);
+ }
+
+ var i = 0;
+ function xonmessage(e) {
+ var msg = JSON.parse(e);
+ var td = (new Date()).getTime() - msg.t;
+ $('#latency').text('' + i + ' ' + td + ' ms');
+ i += 1;
+ send();
+ }
+
+ $('#connect').click(function() {
+ $('#connect').attr('disabled', true);
+ $('#disconnect').each(function(_,e){
+ e.disabled = '';
+ });
+ var transport = $('#transport').val() || undefined;
+ log('[connecting] ' + transport);
+ var url;
+ if ($('#sameOrigin').prop('checked')) {
+ if (window.location.origin) {
+ url = window.location.origin;
+ } else {
+ url = window.location.protocol + '//' + window.location.hostname +
+ (window.location.port ? ':' + window.location.port : '');
+ }
+ } else {
+ url = clientOptions.url;
+ }
+ worker = new Worker('lib/worker.js');
+ worker.onmessage = function (e) {
+ var msg = JSON.parse(e.data);
+ switch (msg.type) {
+ case 'message':
+ xonmessage(msg.data);
+ break;
+ case 'open':
+ onopen();
+ break;
+ case 'close':
+ onclose(msg.code, msg.reason);
+ break;
+ case 'error':
+ console.error(msg.data);
+ break;
+ default:
+ console.error('unknown type: ' + msg.type);
+ }
+ };
+
+ worker.postMessage(JSON.stringify({ type: 'open', url: url + '/echo', transports: transport }));
+ });
+ $('#disconnect').click(function() {
+ $('#disconnect').attr('disabled', true);
+ log('[disconnecting]');
+ worker.postMessage(JSON.stringify({ type: 'close' }));
+ });
+</script>
+</body>
+</html>
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/sockjs-client.git
More information about the Pkg-javascript-commits
mailing list