-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-buffer.html
More file actions
76 lines (60 loc) · 1.87 KB
/
array-buffer.html
File metadata and controls
76 lines (60 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<html>
<head>
</head>
<body>
<div id="result"></div>
<script>
function str2ab_2(str) {
var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
var bufView = new Uint16Array(buf);
for (var i=0, strLen=str.length; i<strLen; i++) {
//console.log(bufView[i], str.charCodeAt(i));
bufView[i] = str.charCodeAt(i);
}
//console.log("Buffer :", buf, bufView);
return bufView;
}
// Array Buffer To String
// Ref : https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
function ab2str_2(buf) {
return String.fromCharCode.apply(null, new Uint16Array(buf));
}
// START WORKER CODE
var w;
function startWorker() {
if(typeof(Worker) !== "undefined") {
if(typeof(w) == "undefined") {
w = new Worker("js/array_buffer_workers.js");
}
var objData = {
"employeeId": 103,
"name": "Sam Smith",
"url" : "../js/cities.json",
"dateHired": new Date(2006, 11, 15)
};
var str= JSON.stringify(objData);
//console.log("str :", str);
var arbfr = str2ab_2(str);
//console.log("arbfr :", arbfr);
w.postMessage(arbfr.buffer, [arbfr.buffer]);
if (arbfr.byteLength) {
alert('Transferables are not supported in your browser!');
//console.log('USING STRUCTURED CLONE (copy) :(');
} else {
//console.log('USING TRANSFERABLE OBJECTS :)');
}
w.onmessage = function(event) {
console.log(event.data);
//var abToString = ab2str_2(event.data);
var parsing = JSON.parse(abToString);
document.getElementById("result").innerHTML = parsing;
};
}
else {
document.getElementById("result").innerHTML = "Sorry! No Web Worker support.";
}
}
startWorker();
</script>
</body>
</html>