forked from MapGIS/WebClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathISocketService.js
More file actions
123 lines (107 loc) · 3.21 KB
/
ISocketService.js
File metadata and controls
123 lines (107 loc) · 3.21 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
import {
Zondy
} from './Base';
import { Evented } from "./Evented";
import {
SubscribePrefix,
BroadcastPrefix,
SubscribeEvent,
BroadcastEvent
} from "./SocketEvent";
Zondy.Socket.ISocketService = undefined;
/**
* Socket的基类
* @author 潘卓然ParnDeedlit 基础平台/创新中心
*
* @alias ISocketService
* @constructor
*
* @param {Object} url Socket流地址的url:
* @param {Number} options 其他预留参数.
*
* @example
* 固定url ws://{ip}:{socket}/websocket/{servicename}
* 服务名 streamdemo
* 消息接收 ws://192.168.91.121:9382/websocket/streamdemo/subscribe
* 消息发送 ws://192.168.91.121:9382/websocket/streamdemo/broadcast
*
*/
export class ISocketService {
constructor(url, options) {
this.url = url || "";
this.options = options;
this.subscribesocket = null;
this.subscribesocket = null;
this.evented = new Evented();
}
createSubscribe() {
var self = this;
this.subscribesocket = this.connect(this.url + SubscribePrefix);
this.subscribesocket.onopen = function(event) {
event.eventType = SubscribeEvent.OPEN;
self.evented.fire(SubscribeEvent.OPEN, event);
};
this.subscribesocket.onmessage = function(event) {
var feature = JSON.parse(event.data);
event.feature = feature;
event.eventType = SubscribeEvent.MESSAGE;
self.evented.fire(SubscribeEvent.MESSAGE, event);
};
this.subscribesocket.onclose = function(event) {
event.eventType = SubscribeEvent.CLOSE;
self.evented.fire(SubscribeEvent.CLOSE, event);
};
this.subscribesocket.onerror = function(event) {
event.eventType = SubscribeEvent.ERROR;
self.evented.fire(SubscribeEvent.ERROR, event);
};
}
createBroadcast() {
var self = this;
this.broadcastsocket = this.connect(this.url + BroadcastPrefix);
this.broadcastsocket.onopen = function(event) {
event.eventType = BroadcastEvent.OPEN;
self.evented.fire(BroadcastEvent.OPEN, event);
};
this.broadcastsocket.onmessage = function(event) {
var feature = JSON.parse(event.data);
event.feature = feature;
event.eventType = BroadcastEvent.MESSAGE;
self.evented.fire(BroadcastEvent.MESSAGE, event);
};
this.broadcastsocket.onclose = function(event) {
event.eventType = BroadcastEvent.CLOSE;
self.evented.fire(BroadcastEvent.CLOSE, event);
};
this.broadcastsocket.onerror = function(event) {
event.eventType = BroadcastEvent.ERROR;
self.evented.fire(BroadcastEvent.ERROR, event);
};
}
connect(url) {
var socketFactory =
"MozWebSocket" in window ? window.MozWebSocket : WebSocket;
return new socketFactory(url);
}
broadcast(data) {
if (!this.broadcastWebSocket || !this.broadcastWebSocket.isOpen) {
return;
}
this.broadcastsocket.send(JSON.stringify(data));
}
closeSubscribe() {
this.subscribesocket.close();
this.subscribesocket = null;
}
closeBroadcast() {
this.broadcastsocket.close();
this.broadcastsocket = null;
}
close() {
this.subscribesocket.close();
this.broadcastsocket.close();
this.subscribesocket = null;
this.broadcastsocket = null;
}
}
Zondy.Socket.ISocketService = ISocketService;