-
Notifications
You must be signed in to change notification settings - Fork 63
/
BrowserChannel.js
115 lines (104 loc) · 4.54 KB
/
BrowserChannel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*!
GPII BrowserChannel Handler
Copyright 2014, 2015 Emergya
Copyright 2015-2018 Raising the Floor - International
Copyright 2020 OCAD University
Licensed under the New BSD license. You may not use this file except in
compliance with this License.
You may obtain a copy of the License at
https://github.com/gpii/universal/LICENSE.txt
*/
"use strict";
var fluid = require("infusion");
var gpii = fluid.registerNamespace("gpii");
fluid.registerNamespace("gpii.flowManager");
fluid.defaults("gpii.flowManager.browserChannel", {
gradeNames: ["fluid.modelComponent"]
});
fluid.defaults("gpii.flowManager.browserChannel.handler", {
gradeNames: ["kettle.request.ws", "gpii.flowManager.sessionAware"],
listeners: {
onReceiveMessage: {
funcName: "gpii.flowManager.browserChannel.receiveMessage",
args: [
"{that}",
"{arguments}.1",
"{flowManager}.solutionsRegistryDataSource",
"{deviceReporter}.platformReporter"
]
}
}
});
/**
* Send an error response for the request.
* @param {Component} request - An instance of gpii.flowManager.browserChannel.handler.
* @param {String} message - Error message text to include in the response.
*/
gpii.flowManager.browserChannel.sendError = function (request, message) {
fluid.log("Sending browserChannel error ", message);
var error = {
isError: true,
message: message
};
request.sendMessage(error);
// These codes bizarrely are not HTTP codes, but listed here: https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
request.ws.close(1008, "Solution id not authorized");
};
/**
* Handler for all message types:
* - an initial "connect" message type establishes the connection and
* initializes the channel and its relationship with the WebSockets settings
* handler. This includes dynamically attaching the
* gpii.flowManager.browserChannel.receiveChangeSettingsMsg() listener to
* handle "changeSettings" message types after the connection is established.
* - any subsequent "connect" message types are silently ignored,
* - all other message types cause an error response and close the connection
* The one exception is the "changeSettings" message type (see first point).
* @param {Component} that - An instance of gpii.flowManager.browserChannel.handler.
* @param {Object} message - Object containing the message type and its payload.
* @param {Component} solutionsRegistryDataSource - Used to match the solution
* given in the payload.
* @param {Component} platformReporter - Used to determine the platform this
* is running on.
*/
gpii.flowManager.browserChannel.receiveMessage = function (that, message, solutionsRegistryDataSource, platformReporter) {
if (message.type !== "connect") {
return;
}
if (that.established) {
gpii.flowManager.browserChannel.sendError(that, "Connection already established - cannot send a second connect message");
}
var solutionId = message.payload.solutionId;
solutionsRegistryDataSource.get({os: platformReporter.reportPlatform().id}, function onSuccess(entries) {
if (!(solutionId in entries)) {
gpii.flowManager.browserChannel.sendError(that, "Rejecting a connection request from '" + solutionId +
"'. The solution id was not found in the solutions registry");
} else {
gpii.settingsHandlers.webSockets.instance.addClient(solutionId, that);
that.established = true;
that.solutionId = solutionId;
}
}, function (error) {
gpii.flowManager.browserChannel.sendError(that, error.message);
});
};
/**
* Listener for the "changeSettings" message type. This is added as a listener
* after the connection has been established. That is, this will not function
* without a previous "connect" message type.
* @param {Component} that - An instance of gpii.flowManager.browserChannel.handler.
* @param {Object} message - Object containing the message type and its payload.
*/
gpii.flowManager.browserChannel.receiveChangeSettingsMsg = function (that, message) {
if (message.type === "changeSettings" && that.established) {
var wsPayload = {};
wsPayload[that.solutionId] = [{
options: {
path: that.solutionId,
source: that
},
settings: message.payload.settings
}];
gpii.settingsHandlers.webSockets.set(wsPayload);
}
};