@gbraver-burst-network/browser-sdk 1.5.3
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/lib/auth0/client.js +31 -0
- package/lib/auth0/client.js.flow +22 -0
- package/lib/auth0/login-redirect.js +26 -0
- package/lib/auth0/login-redirect.js.flow +19 -0
- package/lib/browser-sdk/battle-sdk.js +60 -0
- package/lib/browser-sdk/battle-sdk.js.flow +80 -0
- package/lib/browser-sdk/browser-sdk.js +202 -0
- package/lib/browser-sdk/browser-sdk.js.flow +192 -0
- package/lib/http-request/delete-user.js +23 -0
- package/lib/http-request/delete-user.js.flow +18 -0
- package/lib/index.js +13 -0
- package/lib/index.js.flow +4 -0
- package/lib/json/parse.js +21 -0
- package/lib/json/parse.js.flow +16 -0
- package/lib/promise/promise.js +1 -0
- package/lib/promise/promise.js.flow +10 -0
- package/lib/request/battle-progress-polling.js +1 -0
- package/lib/request/battle-progress-polling.js.flow +10 -0
- package/lib/request/enter-casual-match.js +1 -0
- package/lib/request/enter-casual-match.js.flow +12 -0
- package/lib/request/ping.js +1 -0
- package/lib/request/ping.js.flow +6 -0
- package/lib/request/request.js +1 -0
- package/lib/request/request.js.flow +13 -0
- package/lib/request/send-command.js +1 -0
- package/lib/request/send-command.js.flow +14 -0
- package/lib/response/accept-command.js +21 -0
- package/lib/response/accept-command.js.flow +19 -0
- package/lib/response/battle-end.js +23 -0
- package/lib/response/battle-end.js.flow +24 -0
- package/lib/response/battle-progressed.js +24 -0
- package/lib/response/battle-progressed.js.flow +27 -0
- package/lib/response/battle-start.js +28 -0
- package/lib/response/battle-start.js.flow +39 -0
- package/lib/response/entered-casual-match.js +21 -0
- package/lib/response/entered-casual-match.js.flow +19 -0
- package/lib/response/error.js +22 -0
- package/lib/response/error.js.flow +20 -0
- package/lib/response/not-ready-battle-progress.js +21 -0
- package/lib/response/not-ready-battle-progress.js.flow +19 -0
- package/lib/response/pong.js +22 -0
- package/lib/response/pong.js.flow +21 -0
- package/lib/response/suddenly-battle-end.js +21 -0
- package/lib/response/suddenly-battle-end.js.flow +19 -0
- package/lib/response/websocket-api-response.js +1 -0
- package/lib/response/websocket-api-response.js.flow +23 -0
- package/lib/wait/wait.js +18 -0
- package/lib/wait/wait.js.flow +13 -0
- package/lib/websocket/connect.js +30 -0
- package/lib/websocket/connect.js.flow +25 -0
- package/lib/websocket/enter-casual-match.js +35 -0
- package/lib/websocket/enter-casual-match.js.flow +26 -0
- package/lib/websocket/ping.js +31 -0
- package/lib/websocket/ping.js.flow +23 -0
- package/lib/websocket/send-command.js +130 -0
- package/lib/websocket/send-command.js.flow +103 -0
- package/lib/websocket/send-to-api-server.js +16 -0
- package/lib/websocket/send-to-api-server.js.flow +13 -0
- package/lib/websocket/wait-until.js +53 -0
- package/lib/websocket/wait-until.js.flow +51 -0
- package/package.json +58 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 接続完了したWebSocketを返す
|
|
5
|
+
*
|
|
6
|
+
* @param url 接続先のWebSocket
|
|
7
|
+
* @return WebSocket
|
|
8
|
+
*/
|
|
9
|
+
export function connect(url: string): Promise<WebSocket> {
|
|
10
|
+
let handler = null;
|
|
11
|
+
let errorHandler = null;
|
|
12
|
+
const websocket = new WebSocket(url);
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
handler = () => {
|
|
15
|
+
resolve(websocket);
|
|
16
|
+
};
|
|
17
|
+
errorHandler = reject;
|
|
18
|
+
websocket.addEventListener('open', handler);
|
|
19
|
+
websocket.addEventListener('error', errorHandler);
|
|
20
|
+
})
|
|
21
|
+
.finally(() => {
|
|
22
|
+
handler && websocket.removeEventListener('open', handler);
|
|
23
|
+
errorHandler && websocket.removeEventListener('error', errorHandler);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.enterCasualMatch = enterCasualMatch;
|
|
7
|
+
|
|
8
|
+
var _battleStart = require("../response/battle-start");
|
|
9
|
+
|
|
10
|
+
var _waitUntil = require("./wait-until");
|
|
11
|
+
|
|
12
|
+
var _parse = require("../json/parse");
|
|
13
|
+
|
|
14
|
+
var _sendToApiServer = require("./send-to-api-server");
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* カジュアルマッチを開始する
|
|
18
|
+
*
|
|
19
|
+
* @param websocket websocketクライアント
|
|
20
|
+
* @param armdozerId アームドーザID
|
|
21
|
+
* @param pilotId パイロットID
|
|
22
|
+
* @return バトル情報
|
|
23
|
+
*/
|
|
24
|
+
function enterCasualMatch(websocket, armdozerId, pilotId) {
|
|
25
|
+
(0, _sendToApiServer.sendToAPIServer)(websocket, {
|
|
26
|
+
action: 'enter-casual-match',
|
|
27
|
+
armdozerId,
|
|
28
|
+
pilotId
|
|
29
|
+
});
|
|
30
|
+
return (0, _waitUntil.waitUntil)(websocket, (e, resolve) => {
|
|
31
|
+
const data = (0, _parse.parseJSON)(e.data);
|
|
32
|
+
const response = (0, _battleStart.parseBattleStart)(data);
|
|
33
|
+
response && resolve(response);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
import type {BattleStart} from "../response/battle-start";
|
|
4
|
+
import {parseBattleStart} from "../response/battle-start";
|
|
5
|
+
import {waitUntil} from "./wait-until";
|
|
6
|
+
import type {ArmDozerId, PilotId} from "gbraver-burst-core";
|
|
7
|
+
import type {Resolve} from "../promise/promise";
|
|
8
|
+
import {parseJSON} from "../json/parse";
|
|
9
|
+
import {sendToAPIServer} from "./send-to-api-server";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* カジュアルマッチを開始する
|
|
13
|
+
*
|
|
14
|
+
* @param websocket websocketクライアント
|
|
15
|
+
* @param armdozerId アームドーザID
|
|
16
|
+
* @param pilotId パイロットID
|
|
17
|
+
* @return バトル情報
|
|
18
|
+
*/
|
|
19
|
+
export function enterCasualMatch(websocket: WebSocket, armdozerId: ArmDozerId, pilotId: PilotId): Promise<BattleStart> {
|
|
20
|
+
sendToAPIServer(websocket, {action: 'enter-casual-match', armdozerId, pilotId});
|
|
21
|
+
return waitUntil(websocket, (e: MessageEvent, resolve: Resolve<BattleStart>): void => {
|
|
22
|
+
const data = parseJSON(e.data);
|
|
23
|
+
const response = parseBattleStart(data);
|
|
24
|
+
response && resolve(response);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.ping = ping;
|
|
7
|
+
|
|
8
|
+
var _waitUntil = require("./wait-until");
|
|
9
|
+
|
|
10
|
+
var _parse = require("../json/parse");
|
|
11
|
+
|
|
12
|
+
var _pong = require("../response/pong");
|
|
13
|
+
|
|
14
|
+
var _sendToApiServer = require("./send-to-api-server");
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* API サーバへの疎通確認
|
|
18
|
+
*
|
|
19
|
+
* @param websocket websocketクライアント
|
|
20
|
+
* @return APIサーバからの返答内容
|
|
21
|
+
*/
|
|
22
|
+
function ping(websocket) {
|
|
23
|
+
(0, _sendToApiServer.sendToAPIServer)(websocket, {
|
|
24
|
+
action: 'ping'
|
|
25
|
+
});
|
|
26
|
+
return (0, _waitUntil.waitUntil)(websocket, (e, resolve) => {
|
|
27
|
+
const data = (0, _parse.parseJSON)(e.data);
|
|
28
|
+
const response = (0, _pong.parsePong)(data);
|
|
29
|
+
response && resolve(response);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
import {waitUntil} from "./wait-until";
|
|
4
|
+
import type {Resolve} from "../promise/promise";
|
|
5
|
+
import {parseJSON} from "../json/parse";
|
|
6
|
+
import type {Pong} from "../response/pong";
|
|
7
|
+
import {parsePong} from "../response/pong";
|
|
8
|
+
import {sendToAPIServer} from "./send-to-api-server";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* API サーバへの疎通確認
|
|
12
|
+
*
|
|
13
|
+
* @param websocket websocketクライアント
|
|
14
|
+
* @return APIサーバからの返答内容
|
|
15
|
+
*/
|
|
16
|
+
export function ping(websocket: WebSocket): Promise<Pong> {
|
|
17
|
+
sendToAPIServer(websocket, {action: 'ping'});
|
|
18
|
+
return waitUntil(websocket, (e: MessageEvent, resolve: Resolve<Pong>): void => {
|
|
19
|
+
const data = parseJSON(e.data);
|
|
20
|
+
const response = parsePong(data);
|
|
21
|
+
response && resolve(response);
|
|
22
|
+
});
|
|
23
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.sendCommand = sendCommand;
|
|
7
|
+
exports.sendCommandWithPolling = sendCommandWithPolling;
|
|
8
|
+
|
|
9
|
+
var _sendToApiServer = require("./send-to-api-server");
|
|
10
|
+
|
|
11
|
+
var _waitUntil = require("./wait-until");
|
|
12
|
+
|
|
13
|
+
var _battleProgressed = require("../response/battle-progressed");
|
|
14
|
+
|
|
15
|
+
var _parse = require("../json/parse");
|
|
16
|
+
|
|
17
|
+
var _battleEnd = require("../response/battle-end");
|
|
18
|
+
|
|
19
|
+
var _wait = require("../wait/wait");
|
|
20
|
+
|
|
21
|
+
var _notReadyBattleProgress = require("../response/not-ready-battle-progress");
|
|
22
|
+
|
|
23
|
+
var _acceptCommand = require("../response/accept-command");
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* コマンドをAPIサーバに送信する
|
|
27
|
+
* バトル進行、バトル終了がサーバが送信されるまで待機して、その内容を返す
|
|
28
|
+
*
|
|
29
|
+
* @param websocket websocketクライアント
|
|
30
|
+
* @param battleID バトルID
|
|
31
|
+
* @param flowID フローID
|
|
32
|
+
* @param command コマンド
|
|
33
|
+
* @return サーバからのレスポンス
|
|
34
|
+
*/
|
|
35
|
+
function sendCommand(websocket, battleID, flowID, command) {
|
|
36
|
+
(0, _sendToApiServer.sendToAPIServer)(websocket, {
|
|
37
|
+
action: 'send-command',
|
|
38
|
+
battleID,
|
|
39
|
+
flowID,
|
|
40
|
+
command
|
|
41
|
+
});
|
|
42
|
+
return (0, _waitUntil.waitUntil)(websocket, (e, resolve) => {
|
|
43
|
+
const data = (0, _parse.parseJSON)(e.data);
|
|
44
|
+
const battleProgressed = (0, _battleProgressed.parseBattleProgressed)(data);
|
|
45
|
+
|
|
46
|
+
if (battleProgressed) {
|
|
47
|
+
resolve(battleProgressed);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const battleEnd = (0, _battleEnd.parseBattleEnd)(data);
|
|
52
|
+
|
|
53
|
+
if (battleEnd) {
|
|
54
|
+
resolve(battleEnd);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* 更新確認ポーリング付きでAPIサーバにコマンド送信を行う
|
|
61
|
+
*
|
|
62
|
+
* @param websocket Websocketクライアント
|
|
63
|
+
* @param battleID バトルID
|
|
64
|
+
* @param flowID フローID
|
|
65
|
+
* @param command コマンド
|
|
66
|
+
* @return APIサーバからのレスポンス
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
async function sendCommandWithPolling(websocket, battleID, flowID, command) {
|
|
71
|
+
const maxPollingCount = 100;
|
|
72
|
+
const pollingIntervalMilliSec = 3000;
|
|
73
|
+
let pollingCount = 1;
|
|
74
|
+
let lastPollingTime = 0;
|
|
75
|
+
|
|
76
|
+
const battleProgressPolling = () => {
|
|
77
|
+
pollingCount++;
|
|
78
|
+
lastPollingTime = Date.now();
|
|
79
|
+
(0, _sendToApiServer.sendToAPIServer)(websocket, {
|
|
80
|
+
action: 'battle-progress-polling',
|
|
81
|
+
battleID,
|
|
82
|
+
flowID
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
(0, _sendToApiServer.sendToAPIServer)(websocket, {
|
|
87
|
+
action: 'send-command',
|
|
88
|
+
battleID,
|
|
89
|
+
flowID,
|
|
90
|
+
command
|
|
91
|
+
});
|
|
92
|
+
await (0, _waitUntil.waitUntil)(websocket, (e, resolve) => {
|
|
93
|
+
const data = (0, _parse.parseJSON)(e.data);
|
|
94
|
+
const acceptCommand = (0, _acceptCommand.parseAcceptCommand)(data);
|
|
95
|
+
acceptCommand && resolve(acceptCommand);
|
|
96
|
+
});
|
|
97
|
+
battleProgressPolling();
|
|
98
|
+
return (0, _waitUntil.waitUntil)(websocket, async (e, resolve, reject) => {
|
|
99
|
+
const data = (0, _parse.parseJSON)(e.data);
|
|
100
|
+
const notReadyBattleProgress = (0, _notReadyBattleProgress.parseNotReadyBattleProgress)(data);
|
|
101
|
+
const isOverPollingCount = maxPollingCount <= pollingCount;
|
|
102
|
+
|
|
103
|
+
if (notReadyBattleProgress && isOverPollingCount) {
|
|
104
|
+
reject(new Error('max polling count over'));
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (notReadyBattleProgress) {
|
|
109
|
+
const pollingTime = Date.now() - lastPollingTime;
|
|
110
|
+
const waitTime = Math.max(pollingIntervalMilliSec - pollingTime, 0);
|
|
111
|
+
await (0, _wait.wait)(waitTime);
|
|
112
|
+
battleProgressPolling();
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const battleProgressed = (0, _battleProgressed.parseBattleProgressed)(data);
|
|
117
|
+
|
|
118
|
+
if (battleProgressed) {
|
|
119
|
+
resolve(battleProgressed);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const battleEnd = (0, _battleEnd.parseBattleEnd)(data);
|
|
124
|
+
|
|
125
|
+
if (battleEnd) {
|
|
126
|
+
resolve(battleEnd);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
import type {Command} from "gbraver-burst-core";
|
|
4
|
+
import type {BattleProgressed} from "../response/battle-progressed";
|
|
5
|
+
import type {BattleEnd} from "../response/battle-end";
|
|
6
|
+
import {sendToAPIServer} from "./send-to-api-server";
|
|
7
|
+
import {waitUntil} from "./wait-until";
|
|
8
|
+
import type {Reject, Resolve} from "../promise/promise";
|
|
9
|
+
import {parseBattleProgressed} from "../response/battle-progressed";
|
|
10
|
+
import {parseJSON} from "../json/parse";
|
|
11
|
+
import {parseBattleEnd} from "../response/battle-end";
|
|
12
|
+
import {wait} from "../wait/wait";
|
|
13
|
+
import {parseNotReadyBattleProgress} from "../response/not-ready-battle-progress";
|
|
14
|
+
import {parseAcceptCommand} from "../response/accept-command";
|
|
15
|
+
import type {AcceptCommand} from "../response/accept-command";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* コマンドをAPIサーバに送信する
|
|
19
|
+
* バトル進行、バトル終了がサーバが送信されるまで待機して、その内容を返す
|
|
20
|
+
*
|
|
21
|
+
* @param websocket websocketクライアント
|
|
22
|
+
* @param battleID バトルID
|
|
23
|
+
* @param flowID フローID
|
|
24
|
+
* @param command コマンド
|
|
25
|
+
* @return サーバからのレスポンス
|
|
26
|
+
*/
|
|
27
|
+
export function sendCommand(websocket: WebSocket, battleID: string, flowID: string, command: Command): Promise<BattleProgressed | BattleEnd> {
|
|
28
|
+
sendToAPIServer(websocket, {action: 'send-command', battleID, flowID, command});
|
|
29
|
+
return waitUntil(websocket, (e: MessageEvent, resolve: Resolve<BattleProgressed | BattleEnd>) => {
|
|
30
|
+
const data = parseJSON(e.data);
|
|
31
|
+
|
|
32
|
+
const battleProgressed = parseBattleProgressed(data);
|
|
33
|
+
if (battleProgressed) {
|
|
34
|
+
resolve(battleProgressed);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const battleEnd = parseBattleEnd(data);
|
|
39
|
+
if (battleEnd) {
|
|
40
|
+
resolve(battleEnd);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 更新確認ポーリング付きでAPIサーバにコマンド送信を行う
|
|
48
|
+
*
|
|
49
|
+
* @param websocket Websocketクライアント
|
|
50
|
+
* @param battleID バトルID
|
|
51
|
+
* @param flowID フローID
|
|
52
|
+
* @param command コマンド
|
|
53
|
+
* @return APIサーバからのレスポンス
|
|
54
|
+
*/
|
|
55
|
+
export async function sendCommandWithPolling(websocket: WebSocket, battleID: string, flowID: string, command: Command): Promise<BattleProgressed | BattleEnd> {
|
|
56
|
+
const maxPollingCount = 100;
|
|
57
|
+
const pollingIntervalMilliSec = 3000;
|
|
58
|
+
let pollingCount = 1;
|
|
59
|
+
let lastPollingTime = 0;
|
|
60
|
+
const battleProgressPolling = () => {
|
|
61
|
+
pollingCount ++;
|
|
62
|
+
lastPollingTime = Date.now();
|
|
63
|
+
sendToAPIServer(websocket, {action: 'battle-progress-polling', battleID, flowID});
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
sendToAPIServer(websocket, {action: 'send-command', battleID, flowID, command});
|
|
67
|
+
await waitUntil(websocket, (e: MessageEvent, resolve: Resolve<AcceptCommand>) => {
|
|
68
|
+
const data = parseJSON(e.data);
|
|
69
|
+
const acceptCommand = parseAcceptCommand(data);
|
|
70
|
+
acceptCommand && resolve(acceptCommand);
|
|
71
|
+
});
|
|
72
|
+
battleProgressPolling();
|
|
73
|
+
return waitUntil(websocket, async (e: MessageEvent, resolve: Resolve<BattleProgressed | BattleEnd>, reject: Reject): Promise<void> => {
|
|
74
|
+
const data = parseJSON(e.data);
|
|
75
|
+
|
|
76
|
+
const notReadyBattleProgress = parseNotReadyBattleProgress(data);
|
|
77
|
+
const isOverPollingCount = maxPollingCount <= pollingCount;
|
|
78
|
+
if (notReadyBattleProgress && isOverPollingCount) {
|
|
79
|
+
reject(new Error('max polling count over'));
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (notReadyBattleProgress) {
|
|
84
|
+
const pollingTime = Date.now() - lastPollingTime;
|
|
85
|
+
const waitTime = Math.max(pollingIntervalMilliSec - pollingTime, 0);
|
|
86
|
+
await wait(waitTime);
|
|
87
|
+
battleProgressPolling();
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const battleProgressed = parseBattleProgressed(data);
|
|
92
|
+
if (battleProgressed) {
|
|
93
|
+
resolve(battleProgressed);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const battleEnd = parseBattleEnd(data);
|
|
98
|
+
if (battleEnd) {
|
|
99
|
+
resolve(battleEnd);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.sendToAPIServer = sendToAPIServer;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* APIサーバにメッセージ送信をする
|
|
10
|
+
*
|
|
11
|
+
* @param websocket WebSocketクライアント
|
|
12
|
+
* @param data 送信内容
|
|
13
|
+
*/
|
|
14
|
+
function sendToAPIServer(websocket, data) {
|
|
15
|
+
websocket.send(JSON.stringify(data));
|
|
16
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
import type {APIServerRequest} from "../request/request";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* APIサーバにメッセージ送信をする
|
|
7
|
+
*
|
|
8
|
+
* @param websocket WebSocketクライアント
|
|
9
|
+
* @param data 送信内容
|
|
10
|
+
*/
|
|
11
|
+
export function sendToAPIServer(websocket: WebSocket, data: APIServerRequest): void {
|
|
12
|
+
websocket.send(JSON.stringify(data));
|
|
13
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.waitUntil = waitUntil;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* messageイベントハンドラ
|
|
10
|
+
*
|
|
11
|
+
* @template X resolveするデータの型
|
|
12
|
+
* @param e messageイベント
|
|
13
|
+
* @param resolve resolve関数
|
|
14
|
+
* @param reject reject関数
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Websocketで特定メッセージを受信するまで待機する
|
|
19
|
+
* 利用イメージは、コード例を参照
|
|
20
|
+
*
|
|
21
|
+
* waitUntil(websocket, (e, resolve, reject) => {
|
|
22
|
+
* const data = JSON.parse(e.data);
|
|
23
|
+
*
|
|
24
|
+
* if (data.action === 'Success') {
|
|
25
|
+
* // 特定のメッセージを受信したら、resolveを呼び出して待機終了する
|
|
26
|
+
* // resolveの引数には外部に渡すオブジェクトを指定する
|
|
27
|
+
* resolve(data);
|
|
28
|
+
* }
|
|
29
|
+
*
|
|
30
|
+
* if (data.action === 'Error') {
|
|
31
|
+
* // 例外を投げたい場合は、rejectを呼び出す
|
|
32
|
+
* // rejectの引数には、例外オブジェクトを指定できる
|
|
33
|
+
* reject(data);
|
|
34
|
+
* }
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* @template X 本関数が返すデータ型
|
|
38
|
+
* @param websocket messageイベントを処理するwebsocket
|
|
39
|
+
* @param messageHandler messageイベントが発火した時のハンドラ
|
|
40
|
+
* @return messageHandler内でresolveされたデータ
|
|
41
|
+
*/
|
|
42
|
+
function waitUntil(websocket, messageHandler) {
|
|
43
|
+
let handler = null;
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
handler = e => {
|
|
46
|
+
messageHandler(e, resolve, reject);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
websocket.addEventListener('message', handler);
|
|
50
|
+
}).finally(() => {
|
|
51
|
+
handler && websocket.removeEventListener('message', handler);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
import type {Reject, Resolve} from "../promise/promise";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* messageイベントハンドラ
|
|
7
|
+
*
|
|
8
|
+
* @template X resolveするデータの型
|
|
9
|
+
* @param e messageイベント
|
|
10
|
+
* @param resolve resolve関数
|
|
11
|
+
* @param reject reject関数
|
|
12
|
+
*/
|
|
13
|
+
export type MessageHandler<X> = (e: MessageEvent, resolve: Resolve<X>, reject: Reject) => mixed;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Websocketで特定メッセージを受信するまで待機する
|
|
17
|
+
* 利用イメージは、コード例を参照
|
|
18
|
+
*
|
|
19
|
+
* waitUntil(websocket, (e, resolve, reject) => {
|
|
20
|
+
* const data = JSON.parse(e.data);
|
|
21
|
+
*
|
|
22
|
+
* if (data.action === 'Success') {
|
|
23
|
+
* // 特定のメッセージを受信したら、resolveを呼び出して待機終了する
|
|
24
|
+
* // resolveの引数には外部に渡すオブジェクトを指定する
|
|
25
|
+
* resolve(data);
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* if (data.action === 'Error') {
|
|
29
|
+
* // 例外を投げたい場合は、rejectを呼び出す
|
|
30
|
+
* // rejectの引数には、例外オブジェクトを指定できる
|
|
31
|
+
* reject(data);
|
|
32
|
+
* }
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* @template X 本関数が返すデータ型
|
|
36
|
+
* @param websocket messageイベントを処理するwebsocket
|
|
37
|
+
* @param messageHandler messageイベントが発火した時のハンドラ
|
|
38
|
+
* @return messageHandler内でresolveされたデータ
|
|
39
|
+
*/
|
|
40
|
+
export function waitUntil<X>(websocket: WebSocket, messageHandler: MessageHandler<X>): Promise<X> {
|
|
41
|
+
let handler = null;
|
|
42
|
+
return new Promise((resolve, reject) => {
|
|
43
|
+
handler = (e: MessageEvent) => {
|
|
44
|
+
messageHandler(e, resolve, reject);
|
|
45
|
+
};
|
|
46
|
+
websocket.addEventListener('message', handler);
|
|
47
|
+
})
|
|
48
|
+
.finally(() => {
|
|
49
|
+
handler && websocket.removeEventListener('message', handler);
|
|
50
|
+
});
|
|
51
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gbraver-burst-network/browser-sdk",
|
|
3
|
+
"version": "1.5.3",
|
|
4
|
+
"description": "gbraver burst browser sdk",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"lib"
|
|
8
|
+
],
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "run-s clean type-check lint test transpile copy-type-definition",
|
|
11
|
+
"type-check": "flow",
|
|
12
|
+
"lint": "eslint src",
|
|
13
|
+
"lint:test": "eslint test",
|
|
14
|
+
"test": "ava -T 600000",
|
|
15
|
+
"transpile": "babel src -d lib/",
|
|
16
|
+
"copy-type-definition": "flow-copy-source src lib",
|
|
17
|
+
"clean": "rimraf lib"
|
|
18
|
+
},
|
|
19
|
+
"author": "Y.Takeuchi",
|
|
20
|
+
"license": "ISC",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@auth0/auth0-spa-js": "^1.19.3",
|
|
23
|
+
"@gbraver-burst-network/browser-core": "^1.5.3",
|
|
24
|
+
"gbraver-burst-core": "^1.11.2",
|
|
25
|
+
"rxjs": "^7.4.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@ava/babel": "^2.0.0",
|
|
29
|
+
"@babel/cli": "^7.16.0",
|
|
30
|
+
"@babel/core": "^7.16.5",
|
|
31
|
+
"@babel/eslint-parser": "^7.16.5",
|
|
32
|
+
"@babel/preset-env": "^7.16.5",
|
|
33
|
+
"@babel/preset-flow": "^7.16.5",
|
|
34
|
+
"@babel/register": "^7.16.5",
|
|
35
|
+
"ava": "^3.15.0",
|
|
36
|
+
"babel-loader": "^8.2.3",
|
|
37
|
+
"eslint": "^8.5.0",
|
|
38
|
+
"eslint-plugin-flowtype": "^8.0.3",
|
|
39
|
+
"flow-bin": "^0.168.0",
|
|
40
|
+
"flow-copy-source": "^2.0.9",
|
|
41
|
+
"npm-run-all": "^4.1.5",
|
|
42
|
+
"rimraf": "^3.0.2",
|
|
43
|
+
"webpack": "^5.65.0",
|
|
44
|
+
"webpack-cli": "^4.9.1"
|
|
45
|
+
},
|
|
46
|
+
"ava": {
|
|
47
|
+
"babel": {
|
|
48
|
+
"compileEnhancements": false
|
|
49
|
+
},
|
|
50
|
+
"files": [
|
|
51
|
+
"test/src/**/*.js"
|
|
52
|
+
],
|
|
53
|
+
"require": [
|
|
54
|
+
"@babel/register"
|
|
55
|
+
]
|
|
56
|
+
},
|
|
57
|
+
"gitHead": "c559366efbbe70dc90c9328c3ff83e31f33b62d7"
|
|
58
|
+
}
|