1
1
var websocket = null ; // Websocket to Streamdeck
2
2
var connections = { } ; // Dictionary maintaining connections for each key
3
-
4
- function connect ( coordinates , remoteServer , message ) {
5
- row = coordinates . row ;
6
- column = coordinates . column ;
7
-
8
- key = row + "-" + column ;
9
-
3
+ function addPositionForServer ( server , position ) {
4
+ index = connections [ server ] . positions . indexOf ( position )
5
+ if ( index == - 1 ) {
6
+ connections [ server ] . positions . push ( position )
7
+ }
8
+ }
9
+ function removePositionForServer ( server , position ) {
10
+ index = connections [ server ] . positions . indexOf ( position )
11
+ if ( index != - 1 ) {
12
+ connections [ server ] . positions . splice ( index , 1 )
13
+ }
14
+ }
15
+ function positionFromCoordinates ( c ) {
16
+ return c . column + "-" + c . row
17
+ }
18
+ function connect ( remoteServer , position , message , backend_only = false ) {
10
19
// Close connection if it already exists
11
- if ( connections . hasOwnProperty ( key ) ) {
12
- c = connections [ key ] ;
13
- c . close ( )
20
+ if ( connections . hasOwnProperty ( remoteServer ) && ( backend_only == false ) ) {
21
+ addPositionForServer ( remoteServer , position )
22
+ return
14
23
}
15
24
16
25
// Create a new connection to the remote websocket
17
26
c = new WebSocket ( remoteServer )
18
- connections [ key ] = c
27
+ if ( backend_only ) {
28
+ connections [ remoteServer ] . websocket = c
29
+ }
30
+ else {
31
+ connections [ remoteServer ] = { positions : [ position ] , websocket : c }
32
+ }
19
33
20
34
// send willAppear message when connection is ready
21
35
c . onopen = function ( evt ) {
@@ -36,23 +50,24 @@ function connect(coordinates,remoteServer,message) {
36
50
}
37
51
c . onclose = function ( ) {
38
52
if ( connections . hasOwnProperty ( key ) ) {
39
- connect ( coordinates , remoteServer , null )
53
+ connect ( remoteServer , null , null , true )
40
54
}
41
55
}
42
56
}
43
57
44
- function disconnect ( coordinates , message ) {
45
- row = coordinates . row ;
46
- column = coordinates . column ;
47
- key = row + "-" + column ;
58
+ function disconnect ( remoteServer , position , message ) {
48
59
49
- if ( connections . hasOwnProperty ( key ) ) {
50
- c = connections [ key ] ;
60
+ if ( connections . hasOwnProperty ( remoteServer ) ) {
61
+ c = connections [ remoteServer ] . websocket ;
51
62
if ( c . readyState == 1 ) {
52
63
c . send ( JSON . stringify ( message ) )
53
64
}
54
- delete connections [ key ]
55
- c . close ( )
65
+ removePositionForServer ( remoteServer , position )
66
+ if ( connections [ remoteServer ] . positions . length == 0 ) {
67
+ delete connections [ remoteServer ]
68
+ c . onclose = null
69
+ c . close ( )
70
+ }
56
71
}
57
72
}
58
73
function connectElgatoStreamDeckSocket ( inPort , inPluginUUID , inRegisterEvent , inInfo ) {
@@ -82,24 +97,22 @@ function connectElgatoStreamDeckSocket(inPort, inPluginUUID, inRegisterEvent, in
82
97
console . log ( evt )
83
98
var jsonObj = JSON . parse ( evt . data ) ;
84
99
var event = jsonObj [ 'event' ] ;
85
- var action = jsonObj [ 'action' ] ;
86
- var context = jsonObj [ 'context' ] ;
87
100
88
101
if ( event == "didReceiveSettings" )
89
102
{
90
103
var jsonPayload = jsonObj [ 'payload' ] ;
91
104
var settings = jsonPayload [ 'settings' ] ;
92
105
var coordinates = jsonPayload [ 'coordinates' ] ;
93
106
94
- connect ( coordinates , settings . remoteServer , null )
107
+ connect ( settings . remoteServer , positionFromCoordinates ( coordinates ) )
95
108
}
96
109
else if ( event == "willAppear" )
97
110
{
98
111
var jsonPayload = jsonObj [ 'payload' ] ;
99
112
var settings = jsonPayload [ 'settings' ] ;
100
113
var coordinates = jsonPayload [ 'coordinates' ] ;
101
114
if ( settings . hasOwnProperty ( "remoteServer" ) ) {
102
- connect ( coordinates , settings . remoteServer , jsonObj )
115
+ connect ( settings . remoteServer , positionFromCoordinates ( coordinates ) , jsonObj )
103
116
}
104
117
}
105
118
else if ( event == "willDisappear" )
@@ -108,16 +121,18 @@ function connectElgatoStreamDeckSocket(inPort, inPluginUUID, inRegisterEvent, in
108
121
var settings = jsonPayload [ 'settings' ] ;
109
122
var coordinates = jsonPayload [ 'coordinates' ] ;
110
123
111
- disconnect ( coordinates , jsonObj )
124
+ disconnect ( settings . remoteServer , positionFromCoordinates ( coordinates ) , jsonObj )
112
125
}
113
126
// If there is a key associated with the message, forward to remote websocket
114
127
else if ( jsonObj . hasOwnProperty ( "payload" ) ) {
115
- if ( jsonObj [ 'payload' ] . hasOwnProperty ( "coordinates" ) ) {
116
- key = jsonObj [ 'payload' ] [ 'coordinates' ] [ 'row' ] + "-" + jsonObj [ 'payload' ] [ 'coordinates' ] [ 'column' ]
117
- c = connections [ key ]
118
- if ( c && c . readyState == 1 ) {
119
- console . log ( jsonObj )
120
- c . send ( JSON . stringify ( jsonObj ) )
128
+ if ( jsonObj [ 'payload' ] . hasOwnProperty ( "settings" ) ) {
129
+ key = jsonObj [ 'payload' ] [ 'settings' ] [ 'remoteServer' ]
130
+ if ( connections . hasOwnProperty ( key ) ) {
131
+ c = connections [ key ] . websocket
132
+ if ( c && c . readyState == 1 ) {
133
+ console . log ( jsonObj )
134
+ c . send ( JSON . stringify ( jsonObj ) )
135
+ }
121
136
}
122
137
}
123
138
}
0 commit comments