Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0c719d7

Browse files
committedOct 4, 2018
#71 working on WAMP + ReactPHP
1 parent 221e018 commit 0c719d7

File tree

6 files changed

+290
-28
lines changed

6 files changed

+290
-28
lines changed
 

‎modules/wamp/static-files/wamp/Admin/Tests/TestWampRpcTest.js

+37-16
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ require([
137137

138138
let reason = '';
139139
if (!isError) {
140-
reason = this._onWampConnectOk(data);
140+
reason = this._onWampConnectResolve(data);
141141
} else {
142-
reason = this._onWampConnectError(data);
142+
reason = this._onWampConnectReject(data);
143143
}
144144

145145
if (this.abort) this.wampConnection.close();
@@ -152,27 +152,26 @@ require([
152152
if (!isError) this._runTests();
153153
}
154154

155-
_onWampConnectOk(data) {
155+
_onWampConnectResolve(data) {
156156
console.log('WAMP connection:', data);
157-
this.wampConnection = data;
158157

159158
return 'open';
160159
}
161160

162-
_onWampConnectError(data) {
163-
let reason = 'error';
164-
let reconnect = false;
165-
let isCloseByClient = false;
161+
_onWampConnectReject(data) {
162+
let reason = 'error';
163+
let reconnectionState = 'unknown';
164+
let isClosedByClient = false;
166165
if (data.hasOwnProperty('reason')) {
167-
reason = data.reason;
168-
reconnect = this.wampConnection.isDetailsRetry(data.details);
169-
isCloseByClient = this.wampConnection.isDetailsCloseByClient(data.details);
166+
reason = data.reason;
167+
reconnectionState = this.wampConnection.getDetailsReconnectionState(data.details);
168+
isClosedByClient = this.wampConnection.isDetailsClosedByClient(data.details);
170169
}
171-
if (this.abort) reconnect = false;
170+
if (this.abort) reconnectionState = false;
172171

173-
if (isCloseByClient) {
172+
if (isClosedByClient) {
174173
console.log('WAMP connection closed by client');
175-
}else{
174+
} else {
176175
if (data.hasOwnProperty('reason')) {
177176
console.error(
178177
`WAMP connection. Error:`,
@@ -184,10 +183,10 @@ require([
184183
console.error('WAMP connection. Error:', data);
185184
}
186185

187-
console.log('WAMP connection reconnect:', reconnect);
186+
console.log(`WAMP connection reconnection "${reconnectionState}".`);
188187
}
189188

190-
if (reconnect) this.result.incConnectionTry();
189+
if (reconnectionState) this.result.incConnectionTry();
191190

192191
return reason;
193192
}
@@ -267,4 +266,26 @@ require([
267266
}
268267

269268
new TestWampRpcTestController(rpc);
269+
270+
return;
271+
let wamp = new Wamp(true);
272+
try {
273+
wamp.close();
274+
wamp
275+
.request('api', ['validation', 'userEmail', 'qwe@qwe.qwe'])
276+
.then(response => {
277+
console.log('Request response:', response)
278+
wamp.close();
279+
280+
wamp
281+
.requestApi('validation', 'userEmail', 'qwe2@qwe.qwe')
282+
.then(response => {
283+
console.log('Request response:', response)
284+
})
285+
.catch(error => console.error('Request error:', error));
286+
})
287+
.catch(error => console.error('Request error:', error));
288+
} catch (error) {
289+
console.error('Request system error:', error)
290+
}
270291
});
+234
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
'use strict';
2+
3+
class Wamp {
4+
constructor(debug = false) {
5+
this.debug = debug;
6+
this.connection = undefined;
7+
this.requests = [];
8+
this._requestsOnProgress = false;
9+
10+
this._initOptions();
11+
this._initConnection();
12+
}
13+
14+
_initOptions() {
15+
this.options = {
16+
'lazy': true,
17+
'api_procedure': 'api',
18+
'url': 'wss://' + window.location.hostname + '/wamp',
19+
'realm': 'public',
20+
'cookie_session_name': 'sid',
21+
'cookie_session_separator': '~',
22+
'auth_secret': window.navigator.userAgent,
23+
};
24+
}
25+
26+
_initConnection() {
27+
if (!this.options.lazy) this.connect();
28+
}
29+
30+
_isConnecting() {
31+
return this.connection && this.connection.isOnProgress();
32+
}
33+
34+
_isConnected() {
35+
return this.connection && this.connection.isReady();
36+
}
37+
38+
connect() {
39+
if (this._isConnected()) {
40+
throw new Error('WAMP. Connection already done.');
41+
}
42+
if (this._isConnecting()) {
43+
throw new Error('WAMP. Connection already in progress.');
44+
}
45+
46+
let wampAuthChallenge = this._createAuthChallenge();
47+
48+
let options = this.options;
49+
this._debugNotice(
50+
`Connection:`,
51+
`Url "${options.url}".`,
52+
`Realm "${options.realm}".`,
53+
`Authentication challenge:`, wampAuthChallenge
54+
);
55+
try {
56+
this.connection = new WampConnection(options.url, options.realm, wampAuthChallenge);
57+
this.connection
58+
.onOpen((connection) => this._onConnectResolve(connection))
59+
.onClose((reason, details) => this._onConnectReject(reason, details))
60+
.open();
61+
} catch (error) {
62+
this._onConnectReject('error', error);
63+
}
64+
65+
return this;
66+
}
67+
68+
close() {
69+
if (this._isConnected()) {
70+
this.connection.close();
71+
this._debugNotice(`Connection closing.`);
72+
}
73+
74+
return this;
75+
}
76+
77+
/**
78+
* If return not WampAuthChallenge instance then connection without authentication
79+
*/
80+
_createAuthChallenge() {
81+
let options = this.options;
82+
this._debugNotice(
83+
`Cookie session:`,
84+
`Name "${options.cookie_session_name}".`,
85+
`Separator "${options.cookie_session_separator}".`
86+
);
87+
let wampCookieSession = new WampCookieSession(options.cookie_session_name, options.cookie_session_separator);
88+
89+
this._debugNotice(
90+
`Authentication challenge:`,
91+
`ID "${wampCookieSession.getId()}".`,
92+
`Secret "${options.auth_secret}".`
93+
);
94+
return new WampAuthChallenge(wampCookieSession.getId(), options.auth_secret);
95+
}
96+
97+
_onConnectResolve(connection) {
98+
this._debugNotice(`Connection ready:`, connection);
99+
this._runRequests();
100+
}
101+
102+
_onConnectReject(reason, details) {
103+
let isClosedByClient = false;
104+
let reconnectionState = 'unknown';
105+
let reconnectionTry = 0;
106+
let reconnectionDelay = 0;
107+
if (details.hasOwnProperty('reason')) {
108+
isClosedByClient = this.connection.isDetailsClosedByClient(details);
109+
reconnectionState = this.connection.getDetailsReconnectionState(details);
110+
reconnectionTry = this.connection.getDetailsReconnectionTry(details);
111+
reconnectionDelay = this.connection.getDetailsReconnectionDelay(details);
112+
}
113+
if (isClosedByClient) {
114+
reason = 'closed by client';
115+
}
116+
117+
let message = [
118+
`Connection closed:`,
119+
`Reason "${reason}".`,
120+
];
121+
if (isClosedByClient) {
122+
this._debugNotice.apply(this, message);
123+
} else {
124+
message.concat([
125+
`Details:`, details,
126+
`Reconnection state "${reconnectionState}".`,
127+
`Reconnection try "${reconnectionTry}".`,
128+
`Reconnection delay "${reconnectionDelay}".`,
129+
]);
130+
this._debugError.apply(this, message);
131+
}
132+
}
133+
134+
request(procedure, data = undefined) {
135+
this._debugNotice(
136+
`Request add:`,
137+
`Procedure "${procedure}".`,
138+
`Data:`, data
139+
);
140+
let request = {
141+
'procedure': procedure,
142+
'data': data,
143+
'resolve': undefined,
144+
'reject': undefined,
145+
};
146+
this.requests.push(request);
147+
148+
return new Promise((resolve, reject) => {
149+
request.resolve = resolve;
150+
request.reject = reject;
151+
152+
this._runRequests();
153+
});
154+
}
155+
156+
requestApi(resurce, method, data = undefined) {
157+
data = WampApiRequest.normalizeCallData(data);
158+
data.unshift(method);
159+
data.unshift(resurce);
160+
return this.request(this.options.api_procedure, data);
161+
}
162+
163+
_runRequests() {
164+
if (!this._isConnected()) {
165+
if (this._isConnecting()) return;
166+
return this.connect();
167+
}
168+
169+
if (this._requestsOnProgress) return;
170+
this._requestsOnProgress = true;
171+
172+
for (let i in this.requests) {
173+
if (!this.requests.hasOwnProperty(i)) continue;
174+
let request = this.requests[i];
175+
176+
this._debugNotice(
177+
`Request run:`,
178+
`Procedure "${request.procedure}".`,
179+
`Data:`, request.data
180+
);
181+
try {
182+
new WampRequest(this.connection)
183+
.request(request.procedure, request.data)
184+
.then(response => this._onRequestResolve(request, response))
185+
.catch(error => this._onRequestReject(request, error));
186+
} catch (error) {
187+
this._onRequestReject(error, request.reject);
188+
}
189+
}
190+
191+
this.requests = [];
192+
this._requestsOnProgress = false;
193+
}
194+
195+
_onRequestResolve(request, response) {
196+
this._debugNotice(
197+
`Request response:`,
198+
`Procedure "${request.procedure}".`,
199+
`Data:`, request.data,
200+
`Response:`, response,
201+
);
202+
request.resolve(response);
203+
}
204+
205+
_onRequestReject(request, error) {
206+
this._debugError(
207+
`Request error:`,
208+
`Procedure "${request.procedure}".`,
209+
`Data:`, request.data,
210+
`Error:`, error,
211+
);
212+
request.reject(error);
213+
}
214+
215+
_debugNotice(...args) {
216+
args.unshift(false);
217+
this._debug.apply(this, args);
218+
}
219+
220+
_debugError(...args) {
221+
args.unshift(true);
222+
this._debug.apply(this, args);
223+
}
224+
225+
_debug(isError, ...args) {
226+
if (!this.debug) return;
227+
args.unshift('WAMP.');
228+
let log = Function.prototype.bind.call(
229+
isError ? console.error : console.log,
230+
console
231+
);
232+
log.apply(console, args);
233+
}
234+
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
'use strict';
22

33
class WampApiRequest extends WampRequest {
4-
constructor(connection) {
4+
constructor(connection, procedure = 'api') {
55
super(connection);
6-
this.method = '';
7-
this.data = [];
6+
this.procedure = procedure || 'api';
87
}
98

109
request(resurce, method, data = undefined) {
11-
data = super._normalizeCallData(data);
12-
data.unshift(method)
13-
data.unshift(resurce)
14-
return super.request('api', data);
10+
data = WampApiRequest.normalizeCallData(data);
11+
data.unshift(method);
12+
data.unshift(resurce);
13+
return super.request(this.procedure, data);
1514
}
1615
}

‎modules/wamp/static-files/wamp/WampConnection.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,28 @@ class WampConnection {
1717
this._markAsNotReady();
1818
}
1919

20-
isDetailsRetry(details) {
20+
getDetailsReconnectionState(details) {
2121
if (details.hasOwnProperty('will_retry')) {
2222
return details.will_retry;
2323
}
2424
return false;
2525
}
2626

27-
getDetailsConnectionTry(details) {
27+
getDetailsReconnectionTry(details) {
2828
if (details.hasOwnProperty('retry_count')) {
2929
return details.retry_count;
3030
}
3131
return 0;
3232
}
3333

34-
isDetailsCloseByClient(details) {
34+
getDetailsReconnectionDelay(details) {
35+
if (details.hasOwnProperty('retry_delay')) {
36+
return details.retry_count;
37+
}
38+
return 0;
39+
}
40+
41+
isDetailsClosedByClient(details) {
3542
if (details.hasOwnProperty('reason')) {
3643
return details.reason === 'wamp.error.goodbye_and_out';
3744
}

‎modules/wamp/static-files/wamp/WampRequest.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class WampRequest {
66
}
77

88
request(procedure, data = undefined) {
9-
data = this._normalizeCallData(data);
9+
data = WampRequest.normalizeCallData(data);
1010
return new Promise((resolve, reject) => {
1111
return this.connection
1212
.getSession()
@@ -16,7 +16,7 @@ class WampRequest {
1616
});
1717
}
1818

19-
_normalizeCallData(data) {
19+
static normalizeCallData(data) {
2020
if (data === null || data === undefined) {
2121
return data;
2222
}

‎modules/wamp/views/ifaces/Admin/Tests/TestWampRpcTest.twig

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
{{ js('wamp/WampConnection.js') }}
77
{{ js('wamp/WampRequest.js') }}
88
{{ js('wamp/WampApiRequest.js') }}
9+
{{ js('wamp/Wamp.js') }}
910
{{ js('wamp/Admin/Tests/HtmlNodes.js') }}
1011
{{ js('wamp/Admin/Tests/Stopwatch.js') }}
1112
{{ js('wamp/Admin/Tests/TestWampRpcTestResult.js') }}

0 commit comments

Comments
 (0)
Please sign in to comment.