teleportxr 1.0.7 → 1.0.9

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.
package/client/client.js CHANGED
@@ -130,7 +130,7 @@ class Client {
130
130
  {
131
131
  this.webRtcConnectionManager=WebRtcConnectionManager.getInstance();
132
132
  // We make sure WebRTC has a connection for this client.
133
- this.webRtcConnection = this. webRtcConnectionManager.createConnection(
133
+ this.webRtcConnection = this.webRtcConnectionManager.createConnection(
134
134
  this.clientID
135
135
  ,this.streamingConnectionStateChanged.bind(this)
136
136
  ,this.receivedMessageReliable.bind(this)
@@ -376,9 +376,12 @@ class GeometryService
376
376
  var resource_uids=[];
377
377
  let time_now_us=core.getTimestampUs();
378
378
  for(const [uid, count] of this.streamedMeshes)
379
- {
379
+ {
380
380
  //is mesh streamed
381
381
  var res=GeometryService.GetOrCreateTrackedResource(uid);
382
+ // If it was already received we don't send it:
383
+ if(res.WasAcknowledgedByClient(this.clientID))
384
+ continue;
382
385
  res.Sent(this.clientID,time_now_us);
383
386
  if(res.WasSentToClient(this.clientID))
384
387
  {
@@ -6,7 +6,7 @@ const DefaultRTCPeerConnection = require('@roamhq/wrtc').RTCPeerConnection;
6
6
 
7
7
  const TIME_TO_CONNECTED = 10000;
8
8
  const TIME_TO_HOST_CANDIDATES = 3000; // NOTE: Too long.
9
- const TIME_TO_RECONNECTED = 10000;
9
+ const TIME_TO_RECONNECTED = 1000;
10
10
 
11
11
  function EVEN_ID(id) {
12
12
  return (id-(id%2))
@@ -22,7 +22,7 @@ class WebRtcConnection extends EventEmitter
22
22
  this.id = id;
23
23
  this.state = 'open';
24
24
 
25
- options = {
25
+ this.options = {
26
26
  RTCPeerConnection: DefaultRTCPeerConnection,
27
27
  clearTimeout,
28
28
  setTimeout,
@@ -38,75 +38,110 @@ class WebRtcConnection extends EventEmitter
38
38
  timeToReconnected
39
39
  } = options;
40
40
 
41
- this.connectionStateChanged =options.connectionStateChanged;
41
+ this.connectionStateChanged =options.connectionStateChanged;
42
42
  this.messageReceivedReliableCb =options.messageReceivedReliable;
43
43
  this.messageReceivedUnreliableCb =options.messageReceivedUnreliable;
44
44
 
45
45
  this.sendConfigMessage =options.sendConfigMessage;
46
- const peerConnection =new RTCPeerConnection({ sdpSemantics: 'unified-plan'});
47
- this.pc =peerConnection;
46
+
47
+
48
+ Object.defineProperties(this, {
49
+ iceConnectionState: {
50
+ get ()
51
+ {
52
+ return this.peerConnection.iceConnectionState;
53
+ }
54
+ },
55
+ localDescription: {
56
+ get ()
57
+ {
58
+ return descriptionToJSON(this.peerConnection.localDescription, true);
59
+ }
60
+ },
61
+ remoteDescription: {
62
+ get ()
63
+ {
64
+ return descriptionToJSON(this.peerConnection.remoteDescription);
65
+ }
66
+ },
67
+ signalingState: {
68
+ get ()
69
+ {
70
+ return this.peerConnection.signalingState;
71
+ }
72
+ }
73
+ });
74
+ this.reconnect();
75
+ }
76
+
77
+ reconnect()
78
+ {
79
+ this.peerConnection =new DefaultRTCPeerConnection({ sdpSemantics: 'unified-plan'});
48
80
 
49
- this.beforeOffer(peerConnection);
81
+ this.beforeOffer();
50
82
 
51
- let connectionTimer = options.setTimeout(() =>
83
+ let connectionTimer = this.options.setTimeout(() =>
52
84
  {
53
- if (peerConnection.iceConnectionState !== 'connected'
54
- && peerConnection.iceConnectionState !== 'completed')
85
+ if (this.peerConnection.iceConnectionState !== 'connected'
86
+ && this.peerConnection.iceConnectionState !== 'completed')
55
87
  {
56
88
  this.close();
57
89
  }
58
- }, timeToConnected);
90
+ }, this.options.timeToConnected);
59
91
 
60
92
  let reconnectionTimer = null;
61
93
 
62
94
  const onIceConnectionStateChange = () =>
63
95
  {
64
- console.log("ICE state changed to: "+peerConnection.iceConnectionState);
65
- if (peerConnection.iceConnectionState === 'connected'
66
- || peerConnection.iceConnectionState === 'completed')
96
+ console.log("ICE state changed to: "+this.peerConnection.iceConnectionState);
97
+ if (this.peerConnection.iceConnectionState === 'connected'
98
+ || this.peerConnection.iceConnectionState === 'completed')
67
99
  {
68
100
  if (connectionTimer)
69
101
  {
70
- options.clearTimeout(connectionTimer);
102
+ this.options.clearTimeout(connectionTimer);
71
103
  connectionTimer = null;
72
104
  }
73
- options.clearTimeout(reconnectionTimer);
105
+ this.options.clearTimeout(reconnectionTimer);
74
106
  reconnectionTimer = null;
75
- } else if (peerConnection.iceConnectionState === 'disconnected'
76
- || peerConnection.iceConnectionState === 'failed')
107
+ } else if (this.peerConnection.iceConnectionState === 'disconnected'
108
+ || this.peerConnection.iceConnectionState === 'failed')
77
109
  {
110
+ this.peerConnection.restartIce();
111
+ console.log("restartIce()");
78
112
  if (!connectionTimer && !reconnectionTimer)
79
113
  {
80
114
  const self = this;
81
- reconnectionTimer = options.setTimeout(() =>
115
+ reconnectionTimer = this.options.setTimeout(() =>
82
116
  {
83
- self.close();
84
- }, timeToReconnected);
117
+ this.reconnect();
118
+ this.doOffer();
119
+ }, this.options.timeToReconnected);
85
120
  }
86
121
  }
87
122
  };
88
123
  const onIceGatheringStateChange = () =>
89
124
  {
90
- console.log("ICE gathering state changed to: "+peerConnection.iceGatheringState);
125
+ console.log("ICE gathering state changed to: "+this.peerConnection.iceGatheringState);
91
126
  };
92
127
 
93
- peerConnection.addEventListener('iceconnectionstatechange', onIceConnectionStateChange);
94
- peerConnection.addEventListener('icegatheringstatechange', onIceGatheringStateChange);
128
+ this.peerConnection.addEventListener('iceconnectionstatechange', onIceConnectionStateChange);
129
+ this.peerConnection.addEventListener('icegatheringstatechange', onIceGatheringStateChange);
95
130
 
96
131
 
97
132
  const onConnectionStateChange = () =>
98
- {
99
- console.log("Connection State changed to: "+peerConnection.connectionState.toString());
100
- this.connectionStateChanged(this,peerConnection.connectionState);
133
+ {
134
+ console.log("Connection State changed to: "+this.peerConnection.connectionState.toString());
135
+ this.connectionStateChanged(this,this.peerConnection.connectionState);
101
136
  }
102
- peerConnection.addEventListener("connectionstatechange", onConnectionStateChange);
137
+ this.peerConnection.addEventListener("connectionstatechange", onConnectionStateChange);
103
138
 
104
139
  this.onIceCandidate= ({ candidate })=>
105
140
  {
106
141
  if (!candidate)
107
142
  {
108
- options.clearTimeout(this.timeout);
109
- //peerConnection.removeEventListener('icecandidate', this.onIceCandidate);
143
+ this.options.clearTimeout(this.timeout);
144
+ //this.peerConnection.removeEventListener('icecandidate', this.onIceCandidate);
110
145
  this.deferred.resolve();
111
146
  return;
112
147
  }
@@ -138,7 +173,6 @@ class WebRtcConnection extends EventEmitter
138
173
  this.deferred.reject(new Error('Timed out waiting for host candidates'));
139
174
  }, timeToHostCandidates);
140
175
 
141
-
142
176
  peerConnection.addEventListener('icecandidate', this.onIceCandidate);
143
177
 
144
178
  await this.deferred.promise;
@@ -148,11 +182,11 @@ class WebRtcConnection extends EventEmitter
148
182
  {
149
183
  try
150
184
  {
151
- const offer = await peerConnection.createOffer();
152
- await peerConnection.setLocalDescription(offer);
185
+ const offer = await this.peerConnection.createOffer();
186
+ await this.peerConnection.setLocalDescription(offer);
153
187
  var message = '{"teleport-signal-type":"offer","sdp":"'+offer.sdp+'"}'; //
154
188
  this.sendConfigMessage(this.id,message);
155
- await this.waitUntilIceGatheringStateComplete(peerConnection, options);
189
+ await this.waitUntilIceGatheringStateComplete(this.peerConnection, this.options);
156
190
  } catch (error)
157
191
  {
158
192
  console.error(error.toString());
@@ -174,32 +208,31 @@ class WebRtcConnection extends EventEmitter
174
208
  var sessionDescription=new wrtc.RTCSessionDescription();
175
209
  sessionDescription.sdp=answer;
176
210
  sessionDescription.type="answer";
177
- await peerConnection.setRemoteDescription( sessionDescription);
211
+ await this.peerConnection.setRemoteDescription( sessionDescription);
178
212
  };
179
213
  this.applyRemoteCandidate = async(candidate_txt,mid,mlineindex)=>
180
214
  {
181
215
  console.log("received remote candidate.");
182
- peerConnection.addIceCandidate(new wrtc.RTCIceCandidate({
216
+ this.peerConnection.addIceCandidate(new wrtc.RTCIceCandidate({
183
217
  candidate: candidate_txt,
184
218
  sdpMLineIndex: mlineindex,
185
219
  sdpMid: mid
186
220
  }));
187
221
  };
188
-
189
222
  this.close = () =>
190
223
  {
191
- peerConnection.removeEventListener('iceconnectionstatechange', onIceConnectionStateChange);
224
+ this.peerConnection.removeEventListener('iceconnectionstatechange', onIceConnectionStateChange);
192
225
  if (connectionTimer)
193
226
  {
194
- options.clearTimeout(connectionTimer);
227
+ this.options.clearTimeout(connectionTimer);
195
228
  connectionTimer = null;
196
229
  }
197
230
  if (reconnectionTimer)
198
231
  {
199
- options.clearTimeout(reconnectionTimer);
232
+ this.options.clearTimeout(reconnectionTimer);
200
233
  reconnectionTimer = null;
201
234
  }
202
- peerConnection.close();
235
+ this.peerConnection.close();
203
236
  this.state = 'closed';
204
237
  this.emit('closed');
205
238
  };
@@ -215,33 +248,6 @@ class WebRtcConnection extends EventEmitter
215
248
  signalingState: this.signalingState
216
249
  };
217
250
  };
218
-
219
- Object.defineProperties(this, {
220
- iceConnectionState: {
221
- get ()
222
- {
223
- return peerConnection.iceConnectionState;
224
- }
225
- },
226
- localDescription: {
227
- get ()
228
- {
229
- return descriptionToJSON(peerConnection.localDescription, true);
230
- }
231
- },
232
- remoteDescription: {
233
- get ()
234
- {
235
- return descriptionToJSON(peerConnection.remoteDescription);
236
- }
237
- },
238
- signalingState: {
239
- get ()
240
- {
241
- return peerConnection.signalingState;
242
- }
243
- }
244
- });
245
251
  }
246
252
  sendGeometry(buffer) {
247
253
  try {
@@ -251,7 +257,7 @@ class WebRtcConnection extends EventEmitter
251
257
  console.error('datachannel.sendGeometry exception: '+exception.message);
252
258
  }
253
259
  }
254
- beforeOffer(peerConnection) {
260
+ beforeOffer() {
255
261
 
256
262
  this.videoDataChannel = this.createDataChannel("video",20);
257
263
  this.tagDataChannel = this.createDataChannel("video_tags",40);
@@ -259,7 +265,7 @@ class WebRtcConnection extends EventEmitter
259
265
  this.geometryDataChannel = this.createDataChannel("geometry_unframed",80);
260
266
  this.reliableDataChannel = this.createDataChannel("reliable",100);
261
267
  this.unreliableDataChannel = this.createDataChannel("unreliable",120,false);
262
- // this.dataChannel = peerConnection.createDataChannel('ping-pong',{id:2050});
268
+ // this.dataChannel = this.peerConnection.createDataChannel('ping-pong',{id:2050});
263
269
 
264
270
  function onMessage({ data }) {
265
271
  if (data === 'ping') {
@@ -270,9 +276,9 @@ class WebRtcConnection extends EventEmitter
270
276
  // NOTE(mroberts): This is a hack so that we can get a callback when the
271
277
  // RTCPeerConnection is closed. In the future, we can subscribe to
272
278
  // "connectionstatechange" events.
273
- const { close } = peerConnection;
279
+ const { close } = this.peerConnection;
274
280
  var self=this;
275
- peerConnection.close = function() {
281
+ this.peerConnection.close = function() {
276
282
  //self.dataChannel.removeEventListener('message', onMessage);
277
283
  return close.apply(this, arguments);
278
284
  };
@@ -304,7 +310,7 @@ class WebRtcConnection extends EventEmitter
304
310
  //See https://web.dev/articles/webrtc-datachannels. Can only use id if negotiated=true.
305
311
  const dataChannelOptions={ordered:reliable,maxRetransmits:reliable?10000:0,id:id};
306
312
 
307
- var dc=this.pc.createDataChannel(label,dataChannelOptions);
313
+ var dc=this.peerConnection.createDataChannel(label,dataChannelOptions);
308
314
 
309
315
  dc.onmessage = this.receiveMessage.bind(this,id);
310
316
 
package/index.js CHANGED
@@ -3,11 +3,11 @@ const WebRtcConnectionManager = require('./connections/webrtcconnectionmanager.j
3
3
  const signaling =require("./signaling.js");
4
4
  const client_manager = require('./client/client_manager.js');
5
5
 
6
- function initServer(http_server) {
6
+ function initServer(signaling_port) {
7
7
  var cm=client_manager.getInstance();
8
8
  const webRtcConnectionManager = WebRtcConnectionManager.getInstance();
9
9
  webRtcConnectionManager.SetSendConfigMessage(signaling.sendConfigMessage);
10
- signaling.init(webRtcConnectionManager,cm.newClient.bind(cm),http_server);
10
+ return signaling.init(webRtcConnectionManager,cm.newClient.bind(cm),signaling_port);
11
11
  }
12
12
 
13
13
  module.exports = {initServer}
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  },
14
14
  "name": "teleportxr",
15
15
  "description": "Teleport Spatial Server on node.js",
16
- "version": "1.0.7",
16
+ "version": "1.0.9",
17
17
  "repository": {
18
18
  "type": "git",
19
19
  "url": "git+https://github.com/simul/teleport-nodejs.git"
@@ -29,7 +29,8 @@
29
29
  "./signaling": "./signaling.js",
30
30
  "./client/client_manager": "./client/client_manager.js",
31
31
  "./connections/webrtcconnectionmanager": "./connections/webrtcconnectionmanager.js",
32
- "./scene/scene":"./scene/scene.js"
32
+ "./scene/scene":"./scene/scene.js",
33
+ "./scene/resources":"./scene/resources.js"
33
34
  },
34
35
  "publishConfig": {
35
36
  }
@@ -9,6 +9,11 @@ class Resource
9
9
  //! One trackedResources shared acrosss all clients.
10
10
  static resourcesByUid=new Map();
11
11
  static pathToUid=new Map();
12
+ static defaultPathRoot="http://localhost";
13
+ static SetDefaultPathRoot(str)
14
+ {
15
+ Resource.defaultPathRoot=str;
16
+ }
12
17
  constructor(type,uid,url)
13
18
  {
14
19
  this.uid=uid;
@@ -18,7 +23,10 @@ class Resource
18
23
  encodeIntoDataView(dataView,byteOffset) {
19
24
  byteOffset=core.put_uint8(dataView,byteOffset,this.type);
20
25
  byteOffset=core.put_uint64(dataView,byteOffset,this.uid);
21
- byteOffset=core.put_string(dataView,byteOffset,this.url);
26
+ var url=this.url;
27
+ if(this.url.search("://")==-1)
28
+ url=Resource.defaultPathRoot+this.url;
29
+ byteOffset=core.put_string(dataView,byteOffset,url);
22
30
  return byteOffset;
23
31
  }
24
32
  }
package/signaling.js CHANGED
@@ -214,17 +214,26 @@ function OnWebSocket(ws, req) {
214
214
  console.log("Some Error occurred");
215
215
  };
216
216
  }
217
- exports.init = function (webRtcCM, newClientFn, http_server) {
217
+ exports.init = function (webRtcCM, newClientFn, signaling_port) {
218
218
  // Creating a new websocket server
219
219
  // const signaling_port = process.env.PORT || 8081;
220
- const wss = new WebSocketServer.Server({ http_server });
220
+ var wss;
221
+ if(signaling_port)
222
+ {
223
+ wss= new WebSocketServer.Server({ port: signaling_port});
224
+ }
225
+ else
226
+ {
227
+ wss= new WebSocketServer.Server({ noServer: true });
228
+ }
221
229
  webRtcConnectionManager = webRtcCM;
222
230
  newClient=newClientFn;
223
231
  // Creating connection using websocket
224
232
  wss.on("connection", (ws, req) => {
225
233
  OnWebSocket(ws, req);
226
234
  });
227
- console.log("The WebSockets Signaling Server is running on port " + JSON.stringify(wss.options));
235
+ console.log("The WebSockets Signaling Server is running: " + JSON.stringify(wss.options));
236
+ return wss;
228
237
  };
229
238
  exports.sendConfigMessage = function (clientID, msg) {
230
239
  // Test: is this message valid json?
package/server.js DELETED
@@ -1,35 +0,0 @@
1
- 'use strict';
2
-
3
- const WebRtcConnectionManager = require('./connections/webrtcconnectionmanager');
4
- const cm = require('./client/client_manager.js');
5
-
6
- const signaling=require("./signaling.js");
7
- const scene=require("./scene/scene.js");
8
-
9
- var sc=new scene.Scene();
10
-
11
- const fs = require('fs');
12
- const path = require('path');
13
-
14
- const assetsPath = path.join(__dirname,'assets');
15
-
16
- sc.Load(path.join(assetsPath,'scene.json'));
17
-
18
- // This is our app's callback for when a new client is to be created.
19
- // It must return the origin uid for the client.
20
- function createNewClient(clientID) {
21
- var origin_uid=sc.CreateNode();
22
- return origin_uid;
23
- }
24
-
25
- // This will be called AFTER a client has been created, so we can access it from the clientManager.
26
- function onClientPostCreate(clientID) {
27
- var client=cm.getInstance().GetClient(clientID);
28
- client.SetScene(sc);
29
- }
30
-
31
- const webRtcConnectionManager = WebRtcConnectionManager.getInstance();
32
- webRtcConnectionManager.SetSendConfigMessage(signaling.sendConfigMessage);
33
- cm.getInstance().SetNewClientCallback(createNewClient);
34
- cm.getInstance().SetClientPostCreationCallback(onClientPostCreate);
35
- signaling.init(webRtcConnectionManager,cm.getInstance().newClient.bind(cm.getInstance()));