@yangyongtao/gaea 1.0.3 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/README.md +158 -67
  2. package/package.json +28 -7
  3. package/public/Gaea.apple-touch-icon.png +0 -0
  4. package/public/Gaea.apple-touch-icon.png.import +35 -0
  5. package/public/Gaea.audio.worklet.js +211 -0
  6. package/public/Gaea.html +245 -0
  7. package/public/Gaea.icon.png +0 -0
  8. package/public/Gaea.icon.png.import +35 -0
  9. package/public/Gaea.js +782 -0
  10. package/public/Gaea.png +0 -0
  11. package/public/Gaea.png.import +35 -0
  12. package/public/Gaea.worker.js +1 -0
  13. package/public/GaeaApp.html +275 -0
  14. package/public/GaeaMethods.js +4342 -0
  15. package/public/GaeaModule.html +276 -0
  16. package/public/config.js +76 -0
  17. package/public/constants.js +185 -0
  18. package/public/favicon.ico +0 -0
  19. package/public/image/1.png +0 -0
  20. package/public/image/12-min-min.gif +0 -0
  21. package/public/image/3.png +0 -0
  22. package/public/image/labelImg//345/217/263/350/276/271.png +0 -0
  23. package/public/image/labelImg//345/267/246/350/276/271.png +0 -0
  24. package/public/image/labelImg//346/260/264/344/275/215.png +0 -0
  25. package/public/image/screenIcon//345/205/266/344/273/226.png +0 -0
  26. package/public/image/screenIcon//345/256/211/345/205/250/347/233/221/346/265/213/350/201/232/345/220/210-/347/272/242.png +0 -0
  27. package/public/image/screenIcon//345/256/211/345/205/250/347/233/221/346/265/213/350/201/232/345/220/210-/347/273/277.png +0 -0
  28. package/public/image/screenIcon//346/212/242/351/231/251/345/215/225/344/275/215.png +0 -0
  29. package/public/image/screenIcon//346/226/255/351/235/242.png +0 -0
  30. package/public/image/screenIcon//346/227/240/344/272/272/346/234/272.png +0 -0
  31. package/public/image/screenIcon//346/227/240/344/272/272/346/234/272/345/237/272/345/234/260.png +0 -0
  32. package/public/image/screenIcon//346/260/264/345/272/223/347/253/231.png +0 -0
  33. package/public/image/screenIcon//346/260/264/346/226/207/346/265/213/347/253/231.png +0 -0
  34. package/public/image/screenIcon//346/260/264/351/227/270.png +0 -0
  35. package/public/image/screenIcon//346/262/263/351/201/223/347/253/231-/346/227/240/346/225/260/346/215/256.png +0 -0
  36. package/public/image/screenIcon//346/262/263/351/201/223/347/253/231-/346/255/243/345/270/270.png +0 -0
  37. package/public/image/screenIcon//346/262/263/351/201/223/347/253/231-/350/266/205/344/277/235.png +0 -0
  38. package/public/image/screenIcon//346/262/263/351/201/223/347/253/231-/350/266/205/350/255/246.png +0 -0
  39. package/public/image/screenIcon//346/263/265/347/253/231-/347/201/260.png +0 -0
  40. package/public/image/screenIcon//346/263/265/347/253/231-/347/273/277.png +0 -0
  41. package/public/image/screenIcon//346/265/201/351/207/217/346/265/213/347/253/231.png +0 -0
  42. package/public/image/screenIcon//346/265/267/345/241/230-1/347/272/247.png +0 -0
  43. package/public/image/screenIcon//346/265/267/345/241/230-2/347/272/247.png +0 -0
  44. package/public/image/screenIcon//346/265/267/345/241/230-3/347/272/247.png +0 -0
  45. package/public/image/screenIcon//346/265/267/345/241/230-4/347/272/247.png +0 -0
  46. package/public/image/screenIcon//346/265/267/345/241/230-5/347/272/247.png +0 -0
  47. package/public/image/screenIcon//346/265/267/345/241/230-/346/251/231.png +0 -0
  48. package/public/image/screenIcon//346/265/267/345/241/230-/347/272/242.png +0 -0
  49. package/public/image/screenIcon//346/265/267/345/241/230-/347/273/277.png +0 -0
  50. package/public/image/screenIcon//346/265/267/345/241/230-/350/247/204/346/250/241/344/273/245/344/270/213.png +0 -0
  51. package/public/image/screenIcon//346/265/267/345/241/230-/351/273/204.png +0 -0
  52. package/public/image/screenIcon//346/265/267/345/241/230.png +0 -0
  53. package/public/image/screenIcon//346/275/256/344/275/215.png +0 -0
  54. package/public/image/screenIcon//346/275/256/344/275/215/347/253/231-/346/227/240/346/225/260/346/215/256.png +0 -0
  55. package/public/image/screenIcon//346/275/256/344/275/215/347/253/231-/346/255/243/345/270/270.png +0 -0
  56. package/public/image/screenIcon//346/275/256/344/275/215/347/253/231-/350/266/205/344/277/235.png +0 -0
  57. package/public/image/screenIcon//346/275/256/344/275/215/347/253/231-/350/266/205/350/255/246.png +0 -0
  58. package/public/image/screenIcon//347/211/251/350/265/204/344/273/223/345/272/223.png +0 -0
  59. package/public/image/screenIcon//347/220/203/346/234/272-/345/205/263.png +0 -0
  60. package/public/image/screenIcon//347/220/203/346/234/272-/345/274/200.png +0 -0
  61. package/public/image/screenIcon//347/220/203/346/234/272-/347/272/242.png +0 -0
  62. package/public/image/screenIcon//350/247/206/351/242/221/347/233/221/346/216/247.png +0 -0
  63. package/public/image/screenIcon//351/227/270/351/227/250-/347/201/260.png +0 -0
  64. package/public/image/screenIcon//351/227/270/351/227/250-/347/273/277.png +0 -0
  65. package/public/image/screenIcon//351/230/262/346/264/252/344/273/223/345/272/223.png +0 -0
  66. package/public/image/screenIcon//351/232/220/346/202/243.png +0 -0
  67. package/public/image/screenIcon//351/233/250/351/207/217/347/253/231-/346/265/205/350/223/235.png +0 -0
  68. package/public/image/screenIcon//351/233/250/351/207/217/347/253/231-/347/201/260.png +0 -0
  69. package/public/image/screenIcon//351/233/250/351/207/217/347/253/231-/347/262/211.png +0 -0
  70. package/public/image/screenIcon//351/233/250/351/207/217/347/253/231-/347/272/242.png +0 -0
  71. package/public/image/screenIcon//351/233/250/351/207/217/347/253/231-/347/273/277.png +0 -0
  72. package/public/image/screenIcon//351/233/250/351/207/217/347/253/231-/350/223/235.png +0 -0
  73. package/public/image/screenIcon//351/233/250/351/207/217/347/253/231-/351/273/204.png +0 -0
  74. package/public/image/screenIcon//351/233/250/351/207/217/347/253/231-/351/273/221.png +0 -0
  75. package/public/image/screenIcon//351/243/216/351/200/237.png +0 -0
  76. package/public/image/screenIcon//351/243/216/351/200/237/346/265/213/347/253/231.png +0 -0
  77. package/public/image//346/260/264/346/226/207.gif +0 -0
  78. package/public/image//350/211/262/345/270/246.png +0 -0
  79. package/public/index.html +22 -0
  80. package/public/index.js +185662 -0
  81. package/public/jsWebControl-1.0.0.min.js +31 -0
  82. package/public/jsencrypt.min.js +1 -0
  83. package/public/math/Box2.js +203 -0
  84. package/public/math/Box3.js +532 -0
  85. package/public/math/Color.js +605 -0
  86. package/public/math/ColorManagement.js +74 -0
  87. package/public/math/Cylindrical.js +61 -0
  88. package/public/math/EngineUtils.js +81 -0
  89. package/public/math/Euler.js +315 -0
  90. package/public/math/Frustum.js +162 -0
  91. package/public/math/Interpolant.js +241 -0
  92. package/public/math/Line3.js +115 -0
  93. package/public/math/MathUtils.js +345 -0
  94. package/public/math/Matrix3.js +365 -0
  95. package/public/math/Matrix4.js +858 -0
  96. package/public/math/Plane.js +205 -0
  97. package/public/math/Quaternion.js +724 -0
  98. package/public/math/Ray.js +493 -0
  99. package/public/math/Sphere.js +243 -0
  100. package/public/math/Spherical.js +86 -0
  101. package/public/math/SphericalHarmonics3.js +243 -0
  102. package/public/math/Triangle.js +299 -0
  103. package/public/math/Vector2.js +464 -0
  104. package/public/math/Vector3.js +715 -0
  105. package/public/math/Vector4.js +644 -0
  106. package/public/math/interpolants/CubicInterpolant.js +151 -0
  107. package/public/math/interpolants/DiscreteInterpolant.js +26 -0
  108. package/public/math/interpolants/LinearInterpolant.js +38 -0
  109. package/public/math/interpolants/QuaternionLinearInterpolant.js +39 -0
  110. package/public/static/Cloud.png +0 -0
  111. package/public/static/Global2.jpg +0 -0
  112. package/public/static/css/main.css +170 -0
  113. package/public/static/geojson//346/265/267/345/241/230.geojson +99 -0
  114. package/public/static/img/loading.gif +0 -0
  115. package/public/static/js/app.js +359 -0
  116. package/public/static/js/appcontroller.js +207 -0
  117. package/public/static/js/dist/game.dev.js +137 -0
  118. package/public/static/js/env.js +74 -0
  119. package/public/static/js/event/event.js +107 -0
  120. package/public/static/js/init.js +8 -0
  121. package/public/static/js/initcloudmorph.js +1 -0
  122. package/public/static/js/log.js +35 -0
  123. package/public/static/js/network/ajax.js +29 -0
  124. package/public/static/js/network/rtcp.js +172 -0
  125. package/public/static/js/network/socket.js +106 -0
  126. package/public/web-control.min.js +2 -0
  127. package/scripts/postinstall.mjs +127 -0
  128. package/src/GaeaMethods.js +68 -405
  129. package/src/components/FlyAlongRoute.vue +1 -0
  130. package/src/components/WeatherControl.vue +2 -0
  131. package/src/directives/vDrag.js +70 -0
  132. package/src/index.js +1 -8
  133. package/src/plugin.js +16 -0
  134. package/src/utils.js +10 -21
  135. package/src/vite-plugin.js +117 -0
  136. package/public/static/1111.res +0 -0
  137. package/src/config.js +0 -40
@@ -0,0 +1,359 @@
1
+ const pingIntervalMs = 2000; // 2 secs
2
+ const MOUSE_DOWN = 0;
3
+ const MOUSE_UP = 1;
4
+ const MOUSE_LEFT = 0;
5
+ const MOUSE_RIGHT = 1;
6
+ var isFullscreen = false;
7
+
8
+ // TODO: move to chat.js // Non core logic
9
+ const chatoutput = document.getElementById("chatoutput");
10
+ const chatsubmit = document.getElementById("chatsubmit");
11
+ const username = document.getElementById("chatusername");
12
+ const message = document.getElementById("chatmessage");
13
+ const fullscreen = document.getElementById("fullscreen");
14
+ const appd = document.getElementById("app");
15
+ const chatd = document.getElementById("chat");
16
+ const numplayers = document.getElementById("numplayers");
17
+ const discoverydropdown = document.getElementById("discoverydropdown");
18
+ const appTitle = document.getElementById("appTitle");
19
+
20
+ var offerst;
21
+ // const offer = new RTCSessionDescription(JSON.parse(atob(data)));
22
+ // await pc.setRemoteDescription(offer);
23
+ var appList = [];
24
+
25
+ let isWebRTCInitialized = false;
26
+
27
+ function init() {
28
+ const serverAddress = document.getElementById("serverAddress").value || "192.168.3.58:8080";
29
+ socket.connect(location.protocol, serverAddress).then(() => {
30
+ if (!isWebRTCInitialized) {
31
+ initWebrtc();
32
+ isWebRTCInitialized = true;
33
+ }
34
+ const timeoutMs = 1111;
35
+ const address = `apps`;
36
+ ajax
37
+ .fetch(address, { method: "GET", redirect: "follow" }, timeoutMs)
38
+ .then((data) => {
39
+ data.json().then((body) => {
40
+ updateAppList(body);
41
+ });
42
+ });
43
+ });
44
+ }
45
+
46
+ function send(data) {
47
+ if (conn && conn.readyState === WebSocket.OPEN) {
48
+ conn.send(JSON.stringify(data));
49
+ } else {
50
+ console.error("WebSocket is not open. Current state:", conn ? conn.readyState : "not initialized");
51
+ }
52
+ }
53
+
54
+ // 初始化移至init.js中的按钮点击事件处理
55
+ function initWebrtc() {
56
+ pc = new RTCPeerConnection({
57
+ iceServers: [
58
+ {
59
+ urls: "stun:stun.l.google.com:19302",
60
+ },
61
+ ],
62
+ });
63
+
64
+ // 监控信令状态变化
65
+ pc.onsignalingstatechange = () => {
66
+ console.log(`[RTC] Signaling State: ${pc.signalingState}`);
67
+ };
68
+
69
+ // 监控连接状态变化
70
+ pc.onconnectionstatechange = () => {
71
+ console.log(`[RTC] Connection State: ${pc.connectionState}`);
72
+ };
73
+
74
+ // 增强ICE连接状态监控
75
+ pc.oniceconnectionstatechange = () => {
76
+ console.log(`[RTC] ICE Connection State: ${pc.iceConnectionState}`);
77
+ if (pc.iceConnectionState === 'failed') {
78
+ console.log('[RTC] ICE连接失败,尝试重新协商...');
79
+ pc.restartIce();
80
+ }
81
+ };
82
+
83
+ // 监控ICE收集状态
84
+ pc.onicegatheringstatechange = () => {
85
+ console.log(`[RTC] ICE Gathering State: ${pc.iceGatheringState}`);
86
+ };
87
+
88
+ pc.onicecandidate = (event) => {
89
+ if (event.candidate) {
90
+ console.log('[RTC] 发送新的ICE candidate');
91
+ send({
92
+ type: "CANDIDATE",
93
+ data: JSON.stringify(event.candidate)
94
+ });
95
+ } else {
96
+ console.log('[RTC] ICE candidate收集完成');
97
+ }
98
+ };
99
+
100
+ pc.ontrack = function (event) {
101
+ console.log('[RTC] 收到远程媒体流');
102
+ var el = document.getElementById("app-screen");
103
+ el.srcObject = event.streams[0];
104
+ };
105
+
106
+ pc.addTransceiver("video", {
107
+ direction: "recvonly",
108
+ });
109
+ }
110
+
111
+ function handleOffer(offerData) {
112
+ console.log('[RTC] 处理远程offer');
113
+ const offer = JSON.parse(atob(offerData));
114
+ console.log('[RTC] 当前信令状态:', pc.signalingState);
115
+
116
+ pc.setRemoteDescription(new RTCSessionDescription(offer))
117
+ .then(() => {
118
+ console.log('[RTC] 成功设置远程描述,创建answer');
119
+ return pc.createAnswer();
120
+ })
121
+ .then(answer => {
122
+ console.log('[RTC] 创建answer成功,设置本地描述');
123
+ return pc.setLocalDescription(answer);
124
+ })
125
+ .then(() => {
126
+ console.log('[RTC] 成功设置本地描述,发送answer');
127
+ send({
128
+ type: "ANSWER",
129
+ data: btoa(JSON.stringify(pc.localDescription))
130
+ });
131
+ })
132
+ .catch(err => {
133
+ console.error('[RTC] 处理offer过程出错:', err);
134
+ console.error('[RTC] 错误时的信令状态:', pc.signalingState);
135
+ });
136
+ }
137
+
138
+ // 处理ICE candidate
139
+ function handleCandidate(candidateData) {
140
+ console.log('[RTC] 收到新的ICE candidate');
141
+ try {
142
+ const candidate = JSON.parse(candidateData);
143
+ console.log('[RTC] 解析ICE candidate:', candidate);
144
+
145
+ if (!pc || pc.signalingState === 'closed') {
146
+ console.error('[RTC] 无法添加ICE candidate: RTCPeerConnection未初始化或已关闭');
147
+ return;
148
+ }
149
+
150
+ pc.addIceCandidate(new RTCIceCandidate(candidate))
151
+ .then(() => {
152
+ console.log('[RTC] 成功添加ICE candidate');
153
+ console.log('[RTC] 当前ICE连接状态:', pc.iceConnectionState);
154
+ console.log('[RTC] 当前ICE收集状态:', pc.iceGatheringState);
155
+ })
156
+ .catch(err => {
157
+ console.error('[RTC] 添加ICE candidate失败:', err);
158
+ console.error('[RTC] 失败时的信令状态:', pc.signalingState);
159
+ console.error('[RTC] 失败时的ICE连接状态:', pc.iceConnectionState);
160
+ });
161
+ } catch (err) {
162
+ console.error('[RTC] 解析ICE candidate数据失败:', err);
163
+ }
164
+ }
165
+ const appscreen = document.getElementById("app-screen");
166
+
167
+ // log key
168
+ document.addEventListener("keydown", (e) => {
169
+ if (
170
+ document.activeElement === username ||
171
+ document.activeElement === chatmessage
172
+ ) {
173
+ return;
174
+ }
175
+ send({
176
+ type: "KEYDOWN",
177
+ data: JSON.stringify({
178
+ keyCode: e.keyCode,
179
+ }),
180
+ });
181
+ });
182
+
183
+ document.addEventListener("keyup", (e) => {
184
+ if (
185
+ document.activeElement === username ||
186
+ document.activeElement === chatmessage
187
+ ) {
188
+ return;
189
+ }
190
+ send({
191
+ type: "KEYUP",
192
+ data: JSON.stringify({
193
+ keyCode: e.keyCode,
194
+ }),
195
+ });
196
+ });
197
+
198
+ discoverydropdown.addEventListener("change", () => {
199
+ app = appList[discoverydropdown.selectedIndex];
200
+ socket.connect("http", app.addr).then(() => {
201
+ if (!isWebRTCInitialized) {
202
+ initWebrtc();
203
+ isWebRTCInitialized = true;
204
+ }
205
+ updatePage(app);
206
+ });
207
+ });
208
+
209
+ // Add the event listeners for mousedown, mousemove, and mouseup
210
+ appscreen.addEventListener("mousedown", (e) => {
211
+ x = e.offsetX;
212
+ y = e.offsetY;
213
+ boundRect = appscreen.getBoundingClientRect();
214
+ console.log(e.offsetX, e.offsetY);
215
+ send({
216
+ type: "MOUSEDOWN",
217
+ data: JSON.stringify({
218
+ isLeft: e.button == 0 ? 1 : 0, // 1 is right button
219
+ x: e.offsetX,
220
+ y: e.offsetY,
221
+ width: boundRect.width,
222
+ height: boundRect.height,
223
+ }),
224
+ });
225
+ });
226
+
227
+ appscreen.addEventListener("mouseup", (e) => {
228
+ x = e.offsetX;
229
+ y = e.offsetY;
230
+ boundRect = appscreen.getBoundingClientRect();
231
+ console.log(e.offsetX, e.offsetY);
232
+ send({
233
+ type: "MOUSEUP",
234
+ data: JSON.stringify({
235
+ isLeft: e.button == 0 ? 1 : 0, // 1 is right button
236
+ x: e.offsetX,
237
+ y: e.offsetY,
238
+ width: boundRect.width,
239
+ height: boundRect.height,
240
+ }),
241
+ });
242
+ });
243
+
244
+ appscreen.addEventListener("mousemove", function (e) {
245
+ x = e.offsetX;
246
+ y = e.offsetY;
247
+ boundRect = appscreen.getBoundingClientRect();
248
+ send({
249
+ type: "MOUSEMOVE",
250
+ data: JSON.stringify({
251
+ isLeft: e.button == 0 ? 1 : 0, // 1 is right button
252
+ x: e.offsetX,
253
+ y: e.offsetY,
254
+ width: boundRect.width,
255
+ height: boundRect.height,
256
+ }),
257
+ });
258
+ });
259
+
260
+ appscreen.addEventListener("click", (e) => {
261
+ e.preventDefault();
262
+ return false;
263
+ });
264
+
265
+ chatsubmit.addEventListener("click", (e) => {
266
+ send({
267
+ type: "CHAT",
268
+ data: JSON.stringify({
269
+ user: username.value,
270
+ message: chatmessage.value,
271
+ }),
272
+ });
273
+ });
274
+
275
+ fullscreen.addEventListener("click", (e) => {
276
+ isFullscreen = !isFullscreen;
277
+ if (isFullscreen) {
278
+ chatd.style.display = "none";
279
+ appd.style.display = "flex";
280
+ appd.style.flexDirection = "row";
281
+ appscreen.style.height = "100vh";
282
+ appscreen.style.width = "133.33vh"; // maintain 800x600
283
+ } else {
284
+ chatd.style.display = "block";
285
+ appd.style.display = "block";
286
+ appscreen.style.height = "85vh";
287
+ appscreen.style.width = `${(85 * 8) / 6}vh`; // maintain 800x600
288
+ }
289
+ });
290
+
291
+ function appendChatMessage(data) {
292
+ chatrow = JSON.parse(data.data);
293
+
294
+ var divNode = document.createElement("div");
295
+ var userSpanNode = document.createElement("span");
296
+ var boldNode = document.createElement("strong");
297
+ var messageSpanNode = document.createElement("span");
298
+ userSpanNode.setAttribute("class", "output-user-label");
299
+ messageSpanNode.setAttribute("class", "output-message-label");
300
+ divNode.setAttribute("class", "output-row");
301
+ var userTextnode = document.createTextNode(chatrow.user);
302
+ var messageTextnode = document.createTextNode(chatrow.message);
303
+ boldNode.appendChild(userTextnode);
304
+ userSpanNode.appendChild(boldNode);
305
+ messageSpanNode.appendChild(messageTextnode);
306
+ divNode.appendChild(userSpanNode);
307
+ divNode.appendChild(messageSpanNode);
308
+ chatoutput.appendChild(divNode);
309
+ chatoutput.scrollTop = chatoutput.scrollHeight;
310
+ }
311
+
312
+ function updateNumPlayers(data) {
313
+ sNumPlayers = JSON.parse(data.data);
314
+ numplayers.innerText = "Number of players: " + sNumPlayers;
315
+ }
316
+
317
+ // function isConnectable(addr) {
318
+ // const timeoutMs = 1111;
319
+ // const latency = await ajax.fetch(`${app.addr}/echo`, {method: "GET", redirect: "follow"}, timeoutMs);
320
+ // }
321
+
322
+ function updateAppList(data) {
323
+ console.log(data);
324
+ appList = JSON.parse(data.data);
325
+ discoverydropdown.innerHTML = "";
326
+ const timeoutMs = 1111;
327
+
328
+ Promise.all(
329
+ appList.map((app) => {
330
+ const start = Date.now();
331
+ return ajax
332
+ .fetch(`echo`, { method: "GET", redirect: "follow" }, timeoutMs)
333
+ .then(() => ({ [app.addr]: Date.now() - start }))
334
+ .catch(() => ({ [app.addr]: 9999 }));
335
+ })
336
+ ).then((servers) => {
337
+ const latencies = Object.assign({}, ...servers);
338
+ console.log("[ping] <->", latencies);
339
+
340
+ for (app of appList) {
341
+ appEntry = document.createElement("option");
342
+ appEntry.innerText = app.app_name + "-" + latencies[app.addr] + "ms";
343
+ discoverydropdown.appendChild(appEntry);
344
+ }
345
+ });
346
+ }
347
+
348
+ function updatePage(app) {
349
+ chatd.style.visibility = app.has_chat;
350
+ appTitle.innerText = app.page_title;
351
+ numplayers.style.visibility = app.collaborative;
352
+ }
353
+
354
+ function updateAnswer(data) {
355
+ console.log(`answer ${data.data}`);
356
+ pc.setRemoteDescription(
357
+ new RTCSessionDescription(JSON.parse(atob(data.data)))
358
+ );
359
+ }
@@ -0,0 +1,207 @@
1
+ /**
2
+ * App controller module.
3
+ * @version 1
4
+ */
5
+ (() => {
6
+ const pingIntervalMs = 2000; // 2 secs
7
+ var isFullscreen = false;
8
+
9
+ // TODO: move to chat.js // Non core logic
10
+ const appBody = document.getElementById("app-body");
11
+ const appd = document.getElementById("app");
12
+ const appTitle = document.getElementById("app-title");
13
+ const appScreen = document.getElementById("app-screen");
14
+
15
+ var offerst;
16
+
17
+ const onConnectionReady = () => {
18
+ start();
19
+ };
20
+
21
+ const start = () => {
22
+ if (!rtcp.isConnected()) {
23
+ log.error("App cannot load. Please refresh");
24
+ return;
25
+ }
26
+
27
+ if (!rtcp.isInputReady()) {
28
+ log.error("App is not ready yet. Please wait");
29
+ return;
30
+ }
31
+
32
+ log.info("[control] app start");
33
+
34
+ // TODO: Remove
35
+ // socket.start(gameList.getCurrentGame(), env.isMobileDevice(), room.getId());
36
+
37
+ // // end clear
38
+ // input.poll().enable();
39
+ };
40
+
41
+ const onKeyPress = (data) => {
42
+ rtcp.input(
43
+ JSON.stringify({
44
+ type: "KEYDOWN",
45
+ data: JSON.stringify({
46
+ keyCode: data.key,
47
+ }),
48
+ })
49
+ );
50
+ };
51
+
52
+ const onKeyRelease = (data) => {
53
+ rtcp.input(
54
+ JSON.stringify({
55
+ type: "KEYUP",
56
+ data: JSON.stringify({
57
+ keyCode: data.key,
58
+ }),
59
+ })
60
+ );
61
+ };
62
+
63
+ const onMouseDown = (data) => {
64
+ appScreen.muted = false;
65
+ rtcp.input(
66
+ JSON.stringify({
67
+ type: "MOUSEDOWN",
68
+ data: JSON.stringify(data),
69
+ })
70
+ );
71
+ };
72
+
73
+ const onMouseWheel = (data) => {
74
+ appScreen.muted = false;
75
+ rtcp.input(
76
+ JSON.stringify({
77
+ type: "MOUSEWHEEL",
78
+ data: JSON.stringify(data),
79
+ })
80
+ );
81
+ };
82
+
83
+ const onMouseUp = (data) => {
84
+ rtcp.input(
85
+ JSON.stringify({
86
+ type: "MOUSEUP",
87
+ data: JSON.stringify(data),
88
+ })
89
+ );
90
+ };
91
+ window.onMouseUp = onMouseUp
92
+
93
+ const onMouseMove = (data) => {
94
+ rtcp.input(
95
+ JSON.stringify({
96
+ type: "MOUSEMOVE",
97
+ data: JSON.stringify(data),
98
+ })
99
+ );
100
+ };
101
+
102
+ document.addEventListener("keydown", (e) => {
103
+ //if (
104
+ //document.activeElement === username ||
105
+ //document.activeElement === chatmessage
106
+ //) {
107
+ //return;
108
+ //}
109
+ event.pub(KEY_PRESSED, { key: e.keyCode });
110
+ });
111
+
112
+ document.addEventListener("keyup", (e) => {
113
+ //if (
114
+ //document.activeElement === username ||
115
+ //document.activeElement === chatmessage
116
+ //) {
117
+ //return;
118
+ //}
119
+ event.pub(KEY_RELEASED, { key: e.keyCode });
120
+ });
121
+
122
+ appScreen.addEventListener("wheel", (e) => {
123
+ boundRect = appScreen.getBoundingClientRect();
124
+ event.pub(MOUSE_WHEEL, {
125
+ isLeft: e.button == 0 ? 1 : 0, // 1 is right button
126
+ x: e.clientX - boundRect.left,
127
+ y: e.clientY - boundRect.top,
128
+ width: boundRect.width,
129
+ height: boundRect.height,
130
+ delta: -e.deltaY
131
+ });
132
+ })
133
+
134
+ appScreen.addEventListener("mousedown", (e) => {
135
+ boundRect = appScreen.getBoundingClientRect();
136
+ event.pub(MOUSE_DOWN, {
137
+ isLeft: e.button == 0 ? 1 : 0, // 1 is right button
138
+ x: e.offsetX,
139
+ y: e.offsetY,
140
+ width: boundRect.width,
141
+ height: boundRect.height,
142
+ delta: 0
143
+ });
144
+ });
145
+
146
+ appScreen.addEventListener("mouseup", (e) => {
147
+ boundRect = appScreen.getBoundingClientRect();
148
+ event.pub(MOUSE_UP, {
149
+ isLeft: e.button == 0 ? 1 : 0, // 1 is right button
150
+ x: e.offsetX,
151
+ y: e.offsetY,
152
+ width: boundRect.width,
153
+ height: boundRect.height,
154
+ delta: 0
155
+ });
156
+ });
157
+
158
+ appScreen.addEventListener("mousemove", function (e) {
159
+ boundRect = appScreen.getBoundingClientRect();
160
+ event.pub(MOUSE_MOVE, {
161
+ isLeft: e.button == 0 ? 1 : 0, // 1 is right button
162
+ x: e.clientX - boundRect.left,
163
+ y: e.clientY - boundRect.top,
164
+ width: boundRect.width,
165
+ height: boundRect.height,
166
+ delta: 0
167
+ });
168
+ });
169
+
170
+ document.addEventListener(
171
+ "contextmenu",
172
+ function (e) {
173
+ if (isFullscreen) {
174
+ e.preventDefault();
175
+ }
176
+ },
177
+ false
178
+ );
179
+
180
+ event.sub(MEDIA_STREAM_INITIALIZED, (data) => {
181
+ rtcp.start(data.stunturn);
182
+ });
183
+ event.sub(MEDIA_STREAM_SDP_AVAILABLE, (data) =>
184
+ rtcp.setRemoteDescription(data.sdp, appScreen)
185
+ );
186
+ event.sub(MEDIA_STREAM_CANDIDATE_ADD, (data) =>
187
+ rtcp.addCandidate(data.candidate)
188
+ );
189
+ event.sub(MEDIA_STREAM_CANDIDATE_FLUSH, () => rtcp.flushCandidate());
190
+ event.sub(MEDIA_STREAM_READY, () => rtcp.start());
191
+ event.sub(CONNECTION_READY, onConnectionReady);
192
+ //event.sub(NUM_PLAYER, ({ data }) => updateNumPlayers(data));
193
+ //event.sub(CLIENT_INIT, ({ data }) => {
194
+ //initApps(JSON.parse(data));
195
+ //});
196
+ //event.sub(UPDATE_APP_LIST, ({ data }) => {
197
+ //updateAppList(JSON.parse(data));
198
+ //});
199
+ // event.sub(CONNECTION_CLOSED, () => input.poll().disable());
200
+ event.sub(KEY_PRESSED, onKeyPress);
201
+ event.sub(KEY_RELEASED, onKeyRelease);
202
+ event.sub(MOUSE_MOVE, onMouseMove);
203
+ event.sub(MOUSE_DOWN, onMouseDown);
204
+ event.sub(MOUSE_WHEEL, onMouseWheel);
205
+ event.sub(MOUSE_UP, onMouseUp);
206
+ event.sub(KEY_STATE_UPDATED, (data) => rtcp.input(data));
207
+ })(document, event, env);
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+
3
+ var pingIntervalMs = 2000; // 2 secs
4
+
5
+ var MOUSE_DOWN = 0;
6
+ var MOUSE_UP = 1;
7
+ var MOUSE_LEFT = 0;
8
+ var MOUSE_RIGHT = 1; // const offer = new RTCSessionDescription(JSON.parse(atob(data)));
9
+ // await pc.setRemoteDescription(offer);
10
+
11
+ function init() {
12
+ var address = "".concat(location.protocol !== "https:" ? "ws" : "wss", "://").concat(location.host, "/ws");
13
+ console.info("[ws] connecting to ".concat(address));
14
+ conn = new WebSocket(address); // Clear old roomID
15
+
16
+ conn.onopen = function () {
17
+ console.log("[ws] <- open connection");
18
+ console.log("[ws] -> setting ping interval to ".concat(pingIntervalMs, "ms")); // !to add destructor if SPA
19
+ // setInterval(ping, pingIntervalMs);
20
+ };
21
+
22
+ conn.onerror = function (error) {
23
+ return console.log("[ws] ".concat(error));
24
+ };
25
+
26
+ conn.onclose = function () {
27
+ return console.log("[ws] closed");
28
+ }; // Message received from server
29
+
30
+
31
+ conn.onmessage = function (response) {
32
+ var data = JSON.parse(response.data);
33
+ var message = data.id;
34
+ if (message !== "heartbeat") console.log("[ws] <- message '".concat(message, "' "), data);
35
+
36
+ switch (message) {}
37
+ };
38
+ }
39
+
40
+ function send(data) {
41
+ conn.send(JSON.stringify(data));
42
+ }
43
+
44
+ var pc = new RTCPeerConnection({
45
+ iceServers: [{
46
+ urls: "stun:stun.l.google.com:19302"
47
+ }]
48
+ });
49
+
50
+ pc.oniceconnectionstatechange = function (e) {
51
+ return console.log(pc.iceConnectionState);
52
+ };
53
+
54
+ pc.onicecandidate = function (event) {
55
+ console.log(event.candidate);
56
+ };
57
+
58
+ pc.ontrack = function (event) {
59
+ var el = document.getElementById("app-screen");
60
+ el.srcObject = event.streams[0];
61
+ }; // start session
62
+ // window.startSession = () => {
63
+
64
+
65
+ pc.addTransceiver("video", {
66
+ direction: "recvonly"
67
+ });
68
+ pc.createOffer().then(function (offer) {
69
+ console.log(offer);
70
+ $.post("http://" + location.host + "/signal", btoa(JSON.stringify(offer)), function (data, status) {
71
+ console.log("answer ".concat(data, " and status is ").concat(status));
72
+ pc.setRemoteDescription(new RTCSessionDescription(JSON.parse(atob(data))));
73
+ });
74
+ pc.setLocalDescription(offer);
75
+ }); // .catch(log);
76
+ // }
77
+
78
+ init(); // log key
79
+
80
+ document.addEventListener("keydown", logKey);
81
+
82
+ function logKey(e) {
83
+ console.log(e.keyCode);
84
+ send({
85
+ type: "KEYDOWN",
86
+ data: JSON.stringify({
87
+ keyCode: e.keyCode
88
+ })
89
+ });
90
+ }
91
+
92
+ document.addEventListener("contextmenu", function (event) {
93
+ return event.preventDefault();
94
+ });
95
+ var myPics = document.getElementById("app-screen"); // Add the event listeners for mousedown, mousemove, and mouseup
96
+
97
+ myPics.addEventListener("mouseup", function (e) {
98
+ x = e.offsetX;
99
+ y = e.offsetY;
100
+ b = e.button;
101
+ boundRect = myPics.getBoundingClientRect();
102
+ console.log(e.offsetX, e.offsetY);
103
+ send({
104
+ type: "MOUSE",
105
+ data: JSON.stringify({
106
+ isLeft: e.button == 0 ? 1 : 0,
107
+ // 1 is right button
108
+ isDown: 0,
109
+ x: e.offsetX,
110
+ y: e.offsetY,
111
+ width: boundRect.width,
112
+ height: boundRect.height
113
+ })
114
+ });
115
+ });
116
+ myPics.addEventListener("mousedown", function (e) {
117
+ x = e.offsetX;
118
+ y = e.offsetY;
119
+ boundRect = myPics.getBoundingClientRect();
120
+ console.log(e.offsetX, e.offsetY);
121
+ send({
122
+ type: "MOUSE",
123
+ data: JSON.stringify({
124
+ isLeft: e.button == 0 ? 1 : 0,
125
+ // 1 is right button
126
+ isDown: 1,
127
+ x: e.offsetX,
128
+ y: e.offsetY,
129
+ width: boundRect.width,
130
+ height: boundRect.height
131
+ })
132
+ });
133
+ });
134
+ myPics.addEventListener("click", function (e) {
135
+ e.preventDefault();
136
+ return false;
137
+ });