Skip to content

Commit 861fdf0

Browse files
committed
Use a single websocket for same destination
1 parent 4293014 commit 861fdf0

File tree

2 files changed

+47
-32
lines changed

2 files changed

+47
-32
lines changed

Sources/org.tynsoe.streamdeck.wsproxy.sdPlugin/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"Name": "WebSocket Proxy",
1818
"Icon": "images/websocket",
1919
"SDKVersion": 2,
20-
"Version": "1.1.1",
20+
"Version": "1.1.2",
2121
"PropertyInspectorPath": "plugin/inspector.html",
2222
"OS": [
2323
{

Sources/org.tynsoe.streamdeck.wsproxy.sdPlugin/plugin/index.js

+46-31
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
var websocket = null; // Websocket to Streamdeck
22
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) {
1019
// 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
1423
}
1524

1625
// Create a new connection to the remote websocket
1726
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+
}
1933

2034
// send willAppear message when connection is ready
2135
c.onopen = function(evt) {
@@ -36,23 +50,24 @@ function connect(coordinates,remoteServer,message) {
3650
}
3751
c.onclose = function() {
3852
if (connections.hasOwnProperty(key)) {
39-
connect(coordinates,remoteServer,null)
53+
connect(remoteServer,null,null,true)
4054
}
4155
}
4256
}
4357

44-
function disconnect(coordinates,message) {
45-
row=coordinates.row;
46-
column=coordinates.column;
47-
key=row + "-" + column;
58+
function disconnect(remoteServer,position,message) {
4859

49-
if (connections.hasOwnProperty(key)) {
50-
c=connections[key];
60+
if (connections.hasOwnProperty(remoteServer)) {
61+
c=connections[remoteServer].websocket;
5162
if (c.readyState == 1) {
5263
c.send(JSON.stringify(message))
5364
}
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+
}
5671
}
5772
}
5873
function connectElgatoStreamDeckSocket(inPort, inPluginUUID, inRegisterEvent, inInfo) {
@@ -82,24 +97,22 @@ function connectElgatoStreamDeckSocket(inPort, inPluginUUID, inRegisterEvent, in
8297
console.log(evt)
8398
var jsonObj = JSON.parse(evt.data);
8499
var event = jsonObj['event'];
85-
var action = jsonObj['action'];
86-
var context = jsonObj['context'];
87100

88101
if(event == "didReceiveSettings")
89102
{
90103
var jsonPayload = jsonObj['payload'];
91104
var settings = jsonPayload['settings'];
92105
var coordinates = jsonPayload['coordinates'];
93106

94-
connect(coordinates,settings.remoteServer,null)
107+
connect(settings.remoteServer,positionFromCoordinates(coordinates))
95108
}
96109
else if(event == "willAppear")
97110
{
98111
var jsonPayload = jsonObj['payload'];
99112
var settings = jsonPayload['settings'];
100113
var coordinates = jsonPayload['coordinates'];
101114
if (settings.hasOwnProperty("remoteServer")) {
102-
connect(coordinates,settings.remoteServer,jsonObj)
115+
connect(settings.remoteServer,positionFromCoordinates(coordinates),jsonObj)
103116
}
104117
}
105118
else if(event == "willDisappear")
@@ -108,16 +121,18 @@ function connectElgatoStreamDeckSocket(inPort, inPluginUUID, inRegisterEvent, in
108121
var settings = jsonPayload['settings'];
109122
var coordinates = jsonPayload['coordinates'];
110123

111-
disconnect(coordinates,jsonObj)
124+
disconnect(settings.remoteServer,positionFromCoordinates(coordinates),jsonObj)
112125
}
113126
// If there is a key associated with the message, forward to remote websocket
114127
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+
}
121136
}
122137
}
123138
}

0 commit comments

Comments
 (0)