Skip to content
Permalink
Newer
Older
100644 223 lines (170 sloc) 5.71 KB
1
define(["sugar-web/env"], function (env) {
November 19, 2013 14:06
2
3
'use strict';
4
May 30, 2013 10:15
5
var lastId = 0;
6
var callbacks = {};
August 6, 2013 10:36
7
var notificationCallbacks = {};
8
var client = null;
May 30, 2013 10:15
9
var inputStreams = [];
10
11
function WebSocketClient(environment) {
12
this.queue = [];
13
this.socket = null;
May 30, 2013 10:15
14
15
var that = this;
May 30, 2013 10:15
16
17
env.getEnvironment(function (error, environment) {
June 3, 2013 15:31
18
var port = environment.apiSocketPort;
19
var socket = new WebSocket("ws://0.0.0.0:" + port);
June 3, 2013 15:31
20
21
socket.binaryType = "arraybuffer";
22
23
socket.onopen = function () {
24
var params = [environment.activityId,
25
environment.apiSocketKey];
26
27
socket.send(JSON.stringify({
28
"method": "authenticate",
29
"id": "authenticate",
30
"params": params
31
}));
32
33
while (that.queue.length > 0) {
34
socket.send(that.queue.shift());
May 30, 2013 10:15
35
}
36
};
May 30, 2013 10:15
37
38
socket.onmessage = function (message) {
39
that.onMessage(message);
40
};
May 30, 2013 10:15
41
42
that.socket = socket;
43
});
May 30, 2013 10:15
44
}
45
46
WebSocketClient.prototype.send = function (data) {
47
if (this.socket && this.socket.readyState == WebSocket.OPEN) {
48
this.socket.send(data);
May 30, 2013 10:15
49
} else {
50
this.queue.push(data);
May 30, 2013 10:15
51
}
52
};
53
54
WebSocketClient.prototype.close = function () {
55
this.socket.close();
56
};
May 30, 2013 10:15
57
June 1, 2013 15:06
58
var bus = {};
May 30, 2013 10:15
59
60
function InputStream() {
61
this.streamId = null;
62
this.readCallback = null;
May 30, 2013 10:15
63
}
64
65
InputStream.prototype.open = function (callback) {
66
var that = this;
67
bus.sendMessage("open_stream", [], function (error, result) {
68
that.streamId = result[0];
69
inputStreams[that.streamId] = that;
70
callback(error);
May 30, 2013 10:15
71
});
72
};
73
74
InputStream.prototype.read = function (count, callback) {
75
if (this.readCallback) {
November 19, 2013 14:06
76
throw new Error("Read already in progress");
May 30, 2013 10:15
77
}
78
79
this.readCallback = callback;
May 30, 2013 10:15
80
81
var buffer = new ArrayBuffer(8);
82
83
var headerView = new Uint8Array(buffer, 0, 1);
84
headerView[0] = this.streamId;
85
86
var bodyView = new Uint32Array(buffer, 4, 1);
87
bodyView[0] = count;
88
June 1, 2013 15:06
89
bus.sendBinary(buffer);
May 30, 2013 10:15
90
};
91
92
InputStream.prototype.gotData = function (buffer) {
93
var callback = this.readCallback;
95
this.readCallback = null;
96
97
callback(null, buffer);
May 30, 2013 10:15
98
};
99
100
InputStream.prototype.close = function (callback) {
101
var that = this;
102
103
function onStreamClosed(error, result) {
104
if (callback) {
105
callback(error);
106
}
107
delete inputStreams[that.streamId];
108
}
109
110
bus.sendMessage("close_stream", [this.streamId], onStreamClosed);
May 30, 2013 10:15
111
};
112
113
function OutputStream() {
114
this.streamId = null;
115
}
116
117
OutputStream.prototype.open = function (callback) {
118
var that = this;
119
bus.sendMessage("open_stream", [], function (error, result) {
120
that.streamId = result[0];
121
callback(error);
May 30, 2013 10:15
122
});
123
};
124
125
OutputStream.prototype.write = function (data) {
126
var buffer = new ArrayBuffer(data.byteLength + 1);
127
128
var bufferView = new Uint8Array(buffer);
129
bufferView[0] = this.streamId;
130
bufferView.set(new Uint8Array(data), 1);
131
June 1, 2013 15:06
132
bus.sendBinary(buffer);
May 30, 2013 10:15
133
};
134
135
OutputStream.prototype.close = function (callback) {
136
bus.sendMessage("close_stream", [this.streamId], callback);
May 30, 2013 10:15
137
};
138
June 1, 2013 15:06
139
bus.createInputStream = function (callback) {
May 30, 2013 10:15
140
return new InputStream();
141
};
142
June 1, 2013 15:06
143
bus.createOutputStream = function (callback) {
May 30, 2013 10:15
144
return new OutputStream();
145
};
146
June 1, 2013 15:06
147
bus.sendMessage = function (method, params, callback) {
148
var message = {
May 30, 2013 10:15
149
"method": method,
150
"id": lastId,
September 10, 2013 11:03
151
"params": params,
152
"jsonrpc": "2.0"
May 30, 2013 10:15
153
};
154
155
if (callback) {
156
callbacks[lastId] = callback;
157
}
158
159
client.send(JSON.stringify(message));
May 30, 2013 10:15
160
161
lastId++;
162
};
163
August 6, 2013 10:36
164
bus.onNotification = function (method, callback) {
165
notificationCallbacks[method] = callback;
166
};
167
June 1, 2013 15:06
168
bus.sendBinary = function (buffer, callback) {
169
client.send(buffer);
May 30, 2013 10:15
170
};
171
172
bus.listen = function (customClient) {
173
if (customClient) {
174
client = customClient;
175
} else {
176
client = new WebSocketClient();
177
}
178
179
client.onMessage = function (message) {
180
if (typeof message.data != "string") {
181
var dataView = new Uint8Array(message.data);
182
var streamId = dataView[0];
183
184
if (streamId in inputStreams) {
185
var inputStream = inputStreams[streamId];
186
inputStream.gotData(message.data.slice(1));
187
}
188
189
return;
190
}
191
192
var parsed = JSON.parse(message.data);
193
var responseId = parsed.id;
August 6, 2013 10:36
194
195
if (parsed.method) {
196
var notificationCallback = notificationCallbacks[parsed.method];
197
if (notificationCallback !== undefined) {
198
notificationCallback(parsed.params);
199
}
200
return;
201
}
202
203
if (responseId in callbacks) {
204
var callback = callbacks[responseId];
205
206
if (parsed.error === null) {
207
callback(null, parsed.result);
208
} else {
209
callback(new Error(parsed.error), null);
210
}
211
212
delete callbacks[responseId];
213
}
214
};
June 1, 2013 14:55
215
};
216
June 1, 2013 15:06
217
bus.close = function () {
218
client.close();
219
client = null;
June 1, 2013 14:55
220
};
221
June 1, 2013 15:06
222
return bus;
May 30, 2013 10:15
223
});