Skip to content

Commit

Permalink
Implemented multi-action
Browse files Browse the repository at this point in the history
  • Loading branch information
ybizeul committed Dec 5, 2020
1 parent 577661c commit ad858cc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# v1.1.8
- You can now use SD WebSocket in multi-actions

# v1.1.7
- Properly release unused connections

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"Name": "WebSocket Proxy",
"Icon": "images/websocket",
"SDKVersion": 2,
"Version": "1.1.7",
"Version": "1.1.8",
"PropertyInspectorPath": "plugin/inspector.html",
"OS": [
{
Expand Down
57 changes: 45 additions & 12 deletions Sources/org.tynsoe.streamdeck.wsproxy.sdPlugin/plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ function connect(remoteServer,position,message,backend_only=false) {
}
}

var oneEventConnections = []
function sendOneEvent(remoteServer,message) {
var c = new WebSocket(remoteServer)
oneEventConnections.push(c)
c.onopen = function(evt) {
console.log("Remote multi-action socket opened")
c.send(JSON.stringify(message))
c.close()
delete oneEventConnections[c]
}
}

/*
Disconnects a websocket
Expand Down Expand Up @@ -195,10 +207,16 @@ function connectElgatoStreamDeckSocket(inPort, inPluginUUID, inRegisterEvent, in
var jsonObj = JSON.parse(evt.data);

var event = jsonObj['event'];


var jsonPayload = jsonObj['payload'];
var isInMultiAction = null

if (jsonPayload && jsonPayload.hasOwnProperty("isInMultiAction") && jsonPayload['isInMultiAction']) {
isInMultiAction = jsonPayload['isInMultiAction']
}

if(event == "didReceiveSettings")
{
var jsonPayload = jsonObj['payload'];
var settings = jsonPayload['settings'];
var coordinates = jsonPayload['coordinates'];

Expand All @@ -210,11 +228,18 @@ function connectElgatoStreamDeckSocket(inPort, inPluginUUID, inRegisterEvent, in
*/
else if(event == "willAppear")
{
var jsonPayload = jsonObj['payload'];
var settings = jsonPayload['settings'];
var coordinates = jsonPayload['coordinates'];

if (settings.hasOwnProperty("remoteServer")) {
connect(settings.remoteServer,positionFromCoordinates(coordinates),jsonObj)
// Multiactions are treated as one-offs and have short lived dedicated
// connections
if (isInMultiAction) {
sendOneEvent(settings.remoteServer,jsonObj)
}
else {
connect(settings.remoteServer,positionFromCoordinates(coordinates),jsonObj)
}
}
}
/*
Expand All @@ -223,11 +248,14 @@ function connectElgatoStreamDeckSocket(inPort, inPluginUUID, inRegisterEvent, in
*/
else if(event == "willDisappear")
{
var jsonPayload = jsonObj['payload'];
var settings = jsonPayload['settings'];
var coordinates = jsonPayload['coordinates'];

disconnect(settings.remoteServer,positionFromCoordinates(coordinates),jsonObj)
if (isInMultiAction) {
sendOneEvent(settings.remoteServer,jsonObj)
}
else {
disconnect(settings.remoteServer,positionFromCoordinates(coordinates),jsonObj)
}
}
/*
Every other message is simply forwarded to node-red, the condition is
Expand All @@ -237,11 +265,16 @@ function connectElgatoStreamDeckSocket(inPort, inPluginUUID, inRegisterEvent, in
else if (jsonObj.hasOwnProperty("payload")) {
if (jsonObj['payload'].hasOwnProperty("settings")) {
key = jsonObj['payload']['settings']['remoteServer']
if (connections.hasOwnProperty(key)) {
c=connections[key].websocket
if (c && c.readyState == 1) {
console.log(jsonObj)
c.send(JSON.stringify(jsonObj))
if (isInMultiAction) {
sendOneEvent(key,jsonObj)
}
else {
if (connections.hasOwnProperty(key)) {
c=connections[key].websocket
if (c && c.readyState == 1) {
console.log(jsonObj)
c.send(JSON.stringify(jsonObj))
}
}
}
}
Expand Down

0 comments on commit ad858cc

Please sign in to comment.