forked from webduinoio/webduino-blockly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageTransport.js
More file actions
178 lines (141 loc) · 4.09 KB
/
Copy pathMessageTransport.js
File metadata and controls
178 lines (141 loc) · 4.09 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
+(function (scope) {
'use strict';
var push = Array.prototype.push;
var Transport = scope.Transport,
TransportEvent = scope.TransportEvent,
util = scope.util,
proto;
var STATUS = {
OK: 'OK'
};
var TOPIC = {
PING: '/PING',
PONG: '/PONG',
STATUS: '/STATUS'
};
function MessageTransport(options) {
Transport.call(this, options);
this._options = options;
this._client = null;
this._timer = null;
this._sendTimer = null;
this._buf = [];
this._status = '';
this._connHandler = onConnect.bind(this);
this._messageHandler = onMessage.bind(this);
this._sendOutHandler = sendOut.bind(this);
init(this);
}
function init(self) {
// 這個是拋送訊息用。
// 而用自身的 window 來做資料的接收
self._window = self._options.window || window;
// messageTransport 拋送的通道,固定有 transport: true 來做識別
self._window.postMessage({
transport: true,
transportReady: true
}, location.origin);
window.addEventListener('message', function checkConnect(evt) {
var data = evt.data;
if (evt.origin !== location.origin) return;
if (!data.transport) return;
if (evt.source !== self._window) {
// 這裡一定是相同的,不同就有問題了
console.error('message transport 接收訊息有問題');
return;
}
if (data.transportReady) {
evt.source.postMessage({
transport: true,
transportGo: true
}, location.origin);
window.removeEventListener('message', checkConnect);
self._connHandler();
}
});
}
function onConnect() {
window.addEventListener("message", this._messageHandler, false);
this.isOpen = true;
}
function onMessage(event) {
var message = event.data;
if (event.origin !== location.origin) return;
if (!message.transport) return;
var dest = message.destinationName
var oldStatus = this._status;
var subscribe = [this._options.device + TOPIC.PONG, this._options.device + TOPIC.STATUS]
if (subscribe.indexOf(dest) === -1) {
return;
}
switch (dest.substr(dest.lastIndexOf('/') + 1)) {
case 'STATUS':
this._status = message.payloadString;
detectStatusChange(this, this._status, oldStatus);
break;
default:
(this._status === STATUS.OK) && this.emit(TransportEvent.MESSAGE, message.payloadBytes);
break;
}
}
function detectStatusChange(self, newStatus, oldStatus) {
if (newStatus === oldStatus) {
return;
}
if (newStatus === STATUS.OK) {
self.emit(TransportEvent.OPEN);
} else {
self.emit(TransportEvent.ERROR, new Error('board connection failed.'));
}
}
function sendOut() {
if (this.isOpen) {
this._window.postMessage({
transport: true,
destinationName: this._options.device + TOPIC.PING,
payloadBytes: new Uint8Array(this._buf)
}, location.origin);
}
clearBuf(this);
}
function clearBuf(self) {
self._buf = [];
clearImmediate(self._sendTimer);
self._sendTimer = null;
}
MessageTransport.prototype = proto = Object.create(Transport.prototype, {
constructor: {
value: MessageTransport
},
isOpen: {
get: function () {
return this._isOpen;
},
set: function (val) {
this._isOpen = val;
}
}
});
proto.send = function (payload) {
if (this._buf.length + payload.length + this._options.device.length + TOPIC.PING.length + 4 >
MessageTransport.MAX_PACKET_SIZE) {
this._sendOutHandler();
}
push.apply(this._buf, payload);
if (!this._sendTimer) {
this._sendTimer = setImmediate(this._sendOutHandler);
}
};
proto.close = function () {
window.removeEventListener("message", this._messageHandler);
delete this._window;
delete this._options;
this.isOpen = false;
};
proto.flush = function () {
if (this._buf && this._buf.length) {
this._sendOutHandler();
}
};
scope.transport.message = MessageTransport;
}(webduino));