-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathDataFlowService.js
More file actions
233 lines (209 loc) · 8.1 KB
/
DataFlowService.js
File metadata and controls
233 lines (209 loc) · 8.1 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
* This program are made available under the terms of the Apache License, Version 2.0
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
import {CommonServiceBase} from './CommonServiceBase';
import {Util} from '../commontypes/Util';
import {SecurityManager} from '../security/SecurityManager';
/**
* @class DataFlowService
* @deprecatedclass SuperMap.DataFlowService
* @category iServer DataFlow
* @classdesc 数据流服务类。用于实现客户端与服务器之间实现低延迟和实时数据传输。数据流服务采用 WebSocket 协议,支持全双工、双向式通信。
* 服务器可将流数据服务的分析处理结果作为数据来源向客户端广播,客户端与数据流服务建立连接后,即可自动接收服务器广播的数据。
* @extends {CommonServiceBase}
* @param {string} url - 数据流服务地址。
* @param {Object} options - 参数。
* @param {function} options.style - 设置数据加载样式。
* @param {function} [options.onEachFeature] - 设置每个数据加载 popup 等。
* @param {GeoJSONObject} [options.geometry] - 指定几何范围,只有在该范围内的要素才能被订阅。
* @param {Object} [options.excludeField] - 排除字段。
* @param {boolean} [options.crossOrigin] - 是否允许跨域请求。
* @param {Object} [options.headers] - 请求头。
* @usage
*/
export class DataFlowService extends CommonServiceBase {
constructor(url, options) {
options = options || {};
/*
* @constant EVENT_TYPES
* {Array.<string>}
* 此类支持的事件类型
*/
options.EVENT_TYPES = ["broadcastSocketConnected", "broadcastSocketClosed", "broadcastSocketError", "broadcastFailed", "broadcastSucceeded", "subscribeSocketConnected", "subscribeSocketClosed", "subscribeSocketError", "messageSucceeded", "setFilterParamSucceeded"]
super(url, options);
/**
* @member {GeoJSONObject} DataFlowService.prototype.geometry
* @description 指定几何范围,只有在该范围内的要素才能被订阅。
*/
this.geometry = null;
/**
* @member {Object} DataFlowService.prototype.prjCoordSys
* @description 动态投影参数。
*/
this.prjCoordSys = null;
/**
* @member {Object} DataFlowService.prototype.excludeField
* @description 排除字段。
*/
this.excludeField = null;
Util.extend(this, options);
this.CLASS_NAME = "SuperMap.DataFlowService";
}
/**
* @function DataFlowService.prototype.initBroadcast
* @description 初始化广播。
* @returns {DataFlowService} - 数据流服务。
*/
initBroadcast() {
var me = this;
this.broadcastWebSocket = this._connect(Util.urlPathAppend(me.url, 'broadcast'));
this.broadcastWebSocket.onopen = function (e) {
me.broadcastWebSocket.isOpen = true;
e.eventType = 'broadcastSocketConnected';
me.events.triggerEvent('broadcastSocketConnected', e);
};
this.broadcastWebSocket.onclose = function (e) {
if (me.broadcastWebSocket) {
me.broadcastWebSocket.isOpen = false;
}
e.eventType = 'broadcastSocketClosed';
me.events.triggerEvent('broadcastSocketClosed', e);
};
this.broadcastWebSocket.onerror = function (e) {
e.eventType = 'broadcastSocketError';
me.events.triggerEvent('broadcastSocketError', e);
};
return this;
}
/**
* @function DataFlowService.prototype.broadcast
* @description 加载广播数据。
* @param {GeoJSONObject} geoJSONFeature - JSON 格式的要素数据。
*/
broadcast(geoJSONFeature) {
if (!this.broadcastWebSocket||!this.broadcastWebSocket.isOpen) {
this.events.triggerEvent('broadcastFailed');
return;
}
this.broadcastWebSocket.send(JSON.stringify(geoJSONFeature));
this.events.triggerEvent('broadcastSucceeded');
}
/**
* @function DataFlowService.prototype.initSubscribe
* @description 初始化订阅数据。
* @returns {DataFlowService} DataFlowService 的实例对象。
*/
initSubscribe() {
var me = this;
this.subscribeWebSocket = this._connect(Util.urlPathAppend(me.url, 'subscribe'));
this.subscribeWebSocket.onopen = function (e) {
me.subscribeWebSocket.send(me._getFilterParams());
e.eventType = 'subscribeSocketConnected';
me.events.triggerEvent('subscribeSocketConnected', e);
};
this.subscribeWebSocket.onclose = function (e) {
e.eventType = 'subscribeWebSocketClosed';
me.events.triggerEvent('subscribeWebSocketClosed', e);
};
this.subscribeWebSocket.onerror = function (e) {
e.eventType = 'subscribeSocketError';
me.events.triggerEvent('subscribeSocketError', e);
};
this.subscribeWebSocket.onmessage = function (e) {
me._onMessage(e);
};
return this;
}
/**
* @function DataFlowService.prototype.setExcludeField
* @description 设置排除字段。
* @param {Object} excludeField - 排除字段。
* @returns {DataFlowService} DataFlowService 的实例对象。
*/
setExcludeField(excludeField) {
this.excludeField = excludeField;
this.subscribeWebSocket.send(this._getFilterParams());
return this;
}
/**
* @function DataFlowService.prototype.setGeometry
* @description 设置添加的几何要素数据。
* @param {GeoJSONObject} geometry - 指定几何范围,只有在该范围内的要素才能被订阅。
* @returns {DataFlowService} DataFlowService 的实例对象。
*/
setGeometry(geometry) {
this.geometry = geometry;
this.subscribeWebSocket.send(this._getFilterParams());
return this;
}
/**
* @function DataFlowService.prototype.unSubscribe
* @description 结束订阅数据。
*/
unSubscribe() {
if (!this.subscribeWebSocket) {
return;
}
this.subscribeWebSocket.close();
this.subscribeWebSocket = null;
}
/**
* @function DataFlowService.prototype.unBroadcast
* @description 结束加载广播。
*/
unBroadcast() {
if (!this.broadcastWebSocket) {
return;
}
this.broadcastWebSocket.close();
this.broadcastWebSocket = null;
}
/**
* @function DataFlowService.prototype.destroy
* @override
*/
destroy() {
CommonServiceBase.prototype.destroy.apply(this, arguments);
var me = this;
me.geometry = null;
me.prjCoordSys = null;
me.excludeField = null;
this.unBroadcast();
this.unSubscribe();
}
_getFilterParams() {
var filter = {
filterParam: {
prjCoordSys: this.prjCoordSys,
excludeField: this.excludeField,
geometry: this.geometry
}
};
return Util.toJSON(filter);
}
_onMessage(e) {
if (e.data && e.data.indexOf("filterParam") >= 0) {
var filterParam = JSON.parse(e.data);
e.filterParam = filterParam;
e.eventType = 'setFilterParamSucceeded';
this.events.triggerEvent('setFilterParamSucceeded', e);
return;
}
var feature = JSON.parse(e.data);
e.featureResult = feature;
e.eventType = 'messageSucceeded';
this.events.triggerEvent('messageSucceeded', e);
}
_connect(url) {
url = SecurityManager.appendCredential(url);
if ("WebSocket" in window) {
return new WebSocket(url);
} else if ("MozWebSocket" in window) {
var mozWebSocket = window.MozWebSocket;
return new mozWebSocket(url);
} else {
console.log("no WebSocket");
return null;
}
}
}