0
|
1 define([ "dojo/_base/declare", "dojo/_base/lang", "./Client" ],
|
|
2
|
|
3 function(declare, lang, Client) {
|
|
4 return declare([ Client ], {
|
|
5 _listener : null,
|
|
6
|
|
7 constructor : function(session, destination, options) {
|
|
8 if (!options || !options.listener)
|
|
9 throw new Error("A listener is required");
|
|
10 this._listener = options.listener;
|
|
11 if (options.transform)
|
|
12 this._transform = options.transform;
|
|
13 },
|
|
14
|
|
15 getMode : function() {
|
|
16 return "listener";
|
|
17 },
|
|
18
|
|
19 process : function(result) {
|
|
20 switch (result.type) {
|
|
21 case "message":
|
|
22 try {
|
|
23 this._handleMessage(result.message);
|
|
24 } catch (ex) {
|
|
25 var err = new Error("Failed to handle message");
|
|
26 err.envelope = result.message;
|
|
27 err.innerException = ex;
|
|
28 this._handleError(err);
|
|
29 }
|
|
30 break;
|
|
31 case "error":
|
|
32 this._handleError(result.error);
|
|
33 break;
|
|
34 }
|
|
35
|
|
36 },
|
|
37
|
|
38 _transform : function(envelope) {
|
|
39 return envelope;
|
|
40 },
|
|
41
|
|
42 _handleMessage : function(envelope) {
|
|
43 this.log(
|
|
44 "MESSAGE type = ${0}, headers = ${2}: ${1}",
|
|
45 envelope.bodyType,
|
|
46 envelope.body,
|
|
47 JSON.stringify(envelope.headers));
|
|
48 var data = this._transform(envelope);
|
|
49 this._listener(data);
|
|
50 this.emit("message", data);
|
|
51 },
|
|
52
|
|
53 _handleError : function(ex) {
|
|
54 if (ex.innerException)
|
|
55 this.error(
|
|
56 "ERROR: ${0} -> ${1}",
|
|
57 ex.message,
|
|
58 ex.innerException.message);
|
|
59 else
|
|
60 this.error("ERROR: ${0}", ex.message);
|
|
61 this.emit("error", ex);
|
|
62 }
|
|
63 });
|
|
64 }); |