Skip to content

Commit f65a57a

Browse files
committed
デモページ読み込み時にアクセストークンが無効であれば取得し直す。
1 parent ebf49ef commit f65a57a

1 file changed

Lines changed: 41 additions & 63 deletions

File tree

  • dConnectDevicePlugin/dConnectDeviceMidi/demo/src/libs

dConnectDevicePlugin/dConnectDeviceMidi/demo/src/libs/core.js

Lines changed: 41 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
class Session {
22

3-
constructor(host, scopes, ssl) {
3+
constructor(host, scopes, ssl, useWebSocket) {
44
this._host = host;
55
this._scopes = scopes;
66
this._clientId = null;
77
this._token = null;
88
this._webSocket = null;
9+
this._useWebSocket = (useWebSocket === false) ? false : true;
910
this._ssl = ssl || false;
1011
this._port = 4035;
11-
this._connected = false;
12+
this._wsEstablished = false;
1213
this._pendingOffers = [];
1314
}
1415

1516
get connected() {
16-
return this._connected;
17+
if (this._useWebSocket && !this._wsEstablished) {
18+
return false;
19+
}
20+
return true;
1721
}
1822

1923
get host() {
@@ -40,7 +44,7 @@ class Session {
4044

4145
connect() {
4246
return new Promise((resolve, reject) => {
43-
if (this._connected) {
47+
if (this.connected) {
4448
resolve({ result: 0 });
4549
return;
4650
}
@@ -67,7 +71,7 @@ class Session {
6771
const json = JSON.parse(message);
6872
if (json.result !== undefined) {
6973
if (json.result === 0) {
70-
session._connected = true;
74+
session._wsEstablished = true;
7175
console.log('onmessage: this=', this);
7276
resolve(json);
7377
} else {
@@ -78,7 +82,7 @@ class Session {
7882
socket.onclose = function(event) {
7983
console.log(host + ' - close', event);
8084
session._webSocket = null;
81-
session._connected = false;
85+
session._wsEstablished = false;
8286
};
8387
this._webSocket = socket;
8488
} catch (e) {
@@ -92,7 +96,7 @@ class Session {
9296
this._webSocket.close();
9397
this._webSocket = null;
9498
}
95-
this._connected = false;
99+
this._wsEstablished = false;
96100
}
97101

98102
getRestScheme() {
@@ -105,7 +109,7 @@ class Session {
105109
}
106110

107111
offer(func, params) {
108-
if (this._connected === true) {
112+
if (this.connected === true) {
109113
return func(this, params);
110114
} else {
111115
return new Promise((resolve, reject) => {
@@ -114,13 +118,6 @@ class Session {
114118
}
115119
}
116120

117-
// processPendingOffers() {
118-
// const session = this;
119-
// this._pendingOffers.forEach(offer => {
120-
// func(session, offer.params).then(r => { offer.resolve(r) }).cache(e => { offer.reject(e) })
121-
// })
122-
// }
123-
124121
request(args) {
125122
const method = args.method.toUpperCase();
126123
const path = args.path;
@@ -174,9 +171,6 @@ class Session {
174171
}
175172
}
176173

177-
/**
178-
* Device Connect Client SDK for Javascript.
179-
*/
180174
class DeviceConnectClient {
181175

182176
constructor(op) {
@@ -199,7 +193,7 @@ class DeviceConnectClient {
199193

200194
addSession(args) {
201195
const host = args.host;
202-
const session = new Session(args.host, args.scopes);
196+
const session = new Session(args.host, args.scopes, args.ssl, false);
203197
session.accessToken = args.accessToken;
204198
this._sessions[host] = session;
205199
}
@@ -272,55 +266,45 @@ class DeviceConnectClient {
272266
return new Promise((resolve, reject) => {
273267
let session = this._sessions[host];
274268
if (!session) {
275-
session = new Session(host, scopes, ssl);
269+
session = new Session(host, scopes, ssl, false);
276270
this._sessions[host] = session;
277271
}
278272

279273
// Authorization
280274
this.authorize(session, host, scopes)
281-
282-
// Establish WebSokcet
283275
.then(json => {
284-
console.log('Response:', json);
276+
console.log(json);
277+
let result = json.result;
278+
if (result === 0) {
279+
session.accessToken = json.accessToken;
285280

286-
const result = json.result;
287-
const accessToken = json.accessToken;
288-
if (result === 0 && accessToken) {
289-
console.log('Got Access Token: accessToken=' + accessToken);
290-
const session = this._sessions[host];
291-
session.accessToken = accessToken;
292-
293-
console.log('Connecting to host=' + host);
294-
return session.connect();
281+
this.processPendingOffers(session);
282+
return this.fetchServices(host);
295283
} else {
296-
reject({ what: 'connect', reason: 'no-access-token', errorMessage: '本アプリの使用が認可されませんでした。' });
297-
}
298-
})
299-
.catch(err => {
300-
if (err.code === 4) {
301-
reject({ what: 'connect', reason: 'ws-duplicated', errorMessage: '別ブラウザで既にWebSocketが接続されています。' });
302-
} else if (err.code === 3) {
303-
reject({ what: 'connect', reason: 'ws-invalid-access-token', errorMessage: 'アクセストークンが不正のためにWebSocketを接続できませんでした。' });
304-
} else {
305-
reject({ what: 'connect', reason: 'ws-unknown--error', errorMessage: '不明なエラーによりWebSocketを接続できませんでした。' });
284+
reject({ what: 'connect', reason: 'no-auth', errorMessage: '本アプリケーションの認可に失敗しました。' });
306285
}
307286
})
308287

309-
// Service Discovery
288+
// Service Discovery Result
310289
.then(json => {
311-
console.log(json);
312-
this.processPendingOffers(session);
313-
314-
return this.fetchServices(host);
315-
})
316-
317-
.then(json => {
318-
console.log('Fetched services:', json.services);
290+
if (!json) {
291+
return;
292+
}
319293
const result = json.result;
320294
if (result === 0) {
321295
resolve({session, services:json.services});
322296
} else {
323-
reject({ what: 'connect', reason: 'no-service', errorMessage: 'サービス検索に失敗しました。' });
297+
let errorCode = json.errorCode;
298+
if (11 <= errorCode && errorCode <= 15) {
299+
// 再認可
300+
session.clientId = null;
301+
session.accessToken = null;
302+
this.connect(option)
303+
.then(json => resolve(json))
304+
.catch(err => reject(err));
305+
} else {
306+
reject({ what: 'connect', reason: 'no-service', errorMessage: 'サービス検索に失敗しました。' });
307+
}
324308
}
325309
})
326310
});
@@ -341,21 +325,17 @@ class DeviceConnectClient {
341325

342326
authorize(session, host, scopes) {
343327
if (session.accessToken !== null) {
344-
console.log('AccessToken: ' + session.accessToken);
345328
return Promise.resolve({ result:0, accessToken:session.accessToken });
346329
}
330+
347331
return this.createClient(host)
348-
.then((json) => {
349-
console.log('clientId: ' + json.clientId);
332+
.then(json => {
350333
const result = json.result;
351334
const clientId = json.clientId;
352335
if (result === 0) {
353-
console.log('Created client: clientId=' + clientId);
354-
355-
this._sessions[host].clientId = clientId;
356-
return this.requestAccessToken(host, scopes);
336+
session.clientId = clientId;
337+
return this.requestAccessToken(session, host, scopes);
357338
} else {
358-
console.warn('authorize: createClient: erroCode=' + json.errorCode);
359339
if (json.errorCode === 2) {
360340
// LocalOAuth が OFF の場合は形式的なアクセストークンとして以下の文字列を返す.
361341
// WebSocket 接続確立時に任意の文字列を送信する必要がある.
@@ -379,14 +359,12 @@ class DeviceConnectClient {
379359
});
380360
}
381361

382-
requestAccessToken(host, scopes) {
383-
const session = this._sessions[host];
362+
requestAccessToken(session, host, scopes) {
384363
const params = new URLSearchParams();
385364
params.set('clientId', session.clientId);
386365
params.set('applicationName', this.appName);
387366
params.set('scope', scopes.join(','));
388367
const query = params.toString();
389-
console.log('requestAccessToken: query=' + query)
390368

391369
return fetch("http://" + host + ":4035/gotapi/authorization/accessToken?" + query, {
392370
method: 'GET',

0 commit comments

Comments
 (0)