-
Notifications
You must be signed in to change notification settings - Fork 0
/
TeeSio.js
158 lines (125 loc) · 3.32 KB
/
TeeSio.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
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
const { Ear } = require("@tek-tech/ears");
const path = require('path')
class TeeSio extends Ear{
getSocketClassPath(){
return path.join(__dirname,"TeeSioSocket.js")
}
isListening(){
return this.isListening
}
isReady(){
return this.ready
}
gotSio(){
return this.gotConfig('sio')
}
gotConfig(configname){
return this.config.hasOwnProperty(configname)
}
getConfig(configname){
return (this.gotConfig(configname)) ? this.config[configname] : null
}
gotIo(){
return this.io
}
setIo(){
this.io = this.getConfig('sio')()
}
init(){
if(this.gotSio()){
this.setIo()
const listeners = this.getConfig('listeners')?this.getConfig('listeners'):[]
this.registerSocketListeners(listeners)
this.setReady()
}
else{
console.log('sio object not found..')
}
}
onNewSocket(socket){
const sock = new (require(this.getSocketClassPath()).TeeSioServSocket)(socket)
this.appendSocket(sock)
this.socketlisteners.forEach(
listener=>{
socket.off(
listener[0],(data)=>{
listener[1](data,sock)
}
)
sock.get(
listener[0],(data)=>{
listener[1](data,sock)
}
)
}
)
}
registerSocketListeners(listeners=[]){
this.socketlisteners = listeners
}
registerSocketListener(listener){
this.socketlisteners.push(listener)
}
appendSocket(sock){
this.broadcastlist.push(sock)
if(sock.getUuid()){
if(this.getSockByUuid())this.replaceSock(uuid,sock)
else this.clisockets.push(sock)
}
}
replaceSock(uuid,sock){
this.clisockets.forEach(
(socket,idx)=>{
if(socket.getUuid()==uuid){
sokcet = sock
this.clisockets[idx]=sock
}
}
)
}
getSockByUuid(uuid){
let found = null
this.clisockets.forEach(
sock=>{
if(sock.getUuid()==uuid)found = sock
}
)
return found
}
listen(){
if(this.gotSio()){
if(!this.gotIo()) this.setIo()
this.io.listen(this.gotConfig('usesTeeWeb')?this.getConfig('server').getInstance():this.getConfig('server'))
this.io.on(
'connection',socket=>{
this.onNewSocket(socket)
}
)
this.isListening = true
}
}
assignData(data){
Object.keys(data).forEach(
configname=>{
this.config[configname] = data[configname]
}
)
}
constructor(data){
super()
this.broadcastlist = []
this.socketlisteners = []
this.clisockets = []
this.isListening = false
this.io = null
this.config = {}
this.assignData(data)
this.init()
this.whenReady(
()=>{
console.log("Teesio is ready")
}
)
}
}
module.exports = {TeeSio}