Last active
August 23, 2018 16:03
-
-
Save jsinai/b370e7a3e1bf454e0c58768896405b8c to your computer and use it in GitHub Desktop.
An example of a heartbeat that detects if an html application stops responding, and restarts it. See this project for a full example of how to include a plugin like this in your BrightAuthor application: https://github.com/brightsign/node.js-starter-project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var app = express(); | |
var bsSocketMessage = new BSDatagramSocket(); | |
var intervl = undefined; | |
var udpPort = 5003; | |
var heartBeatFreqMs = 4000; | |
function main() { | |
// For testing: | |
// curl http://[brightsign ip address]:9090/die | |
app.get('/die', function (req, res) { | |
clearInterval(intervl); | |
res.write('dead'); | |
res.end(); | |
}); | |
app.listen(9090, function() { | |
console.log('Example app listening on port 9090!'); | |
}); | |
intervl = setInterval(function(){ | |
bsSocketMessage.SendTo('127.0.0.1', udpPort, 'heartbeat'); | |
console.log('Sending heartbeat'); | |
}, heartBeatFreqMs); | |
} | |
window.main = main; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Node_Initialize(msgPort As Object, userVariables As Object, bsp as Object) | |
print "=== Node_Initialize - entry" | |
h = {} | |
h.version = "1.00.00" | |
h.msgPort = msgPort | |
h.userVariables = userVariables | |
h.bsp = bsp | |
h.ProcessEvent = Node_ProcessEvent | |
h.CreateHtmlWidget = CreateHtmlWidget | |
h.CreateHeartbeatTimer = CreateHeartbeatTimer | |
h.CreateHeartbeatReceiver = CreateHeartbeatReceiver | |
h.objectName = "Node_object" | |
h.heartBeatUdpPort = 5003; | |
h.heartBeatTimerDelaySec = 10; | |
h.CreateHeartbeatTimer() | |
h.CreateHeartbeatReceiver() | |
h.CreateHtmlWidget() | |
return h | |
End Function | |
Function Node_ProcessEvent(event As Object) as boolean | |
if type(event) = "roTimerEvent" and event.GetUserData() <> invalid and event.GetUserData().name = "HeartbeatTimer" then | |
' If this timer fires, it means a heartbeat was not received in time. | |
print "=== Heartbeat timer fired: calling CreateHtmlWidget" | |
m.CreateHtmlWidget() | |
return true | |
else if type(event) = "roDatagramEvent" and event.GetUserData() <> invalid and event.GetUserData().name = "HeartbeatUdp" then | |
print "=== Received UDP, restarting timer " | |
m.CreateHeartbeatTimer() | |
return true | |
end if | |
return false | |
End Function | |
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' | |
Sub CreateHeartbeatTimer() | |
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' | |
if m.heartbeatTimer <> invalid then | |
m.heartbeatTimer.Stop() | |
m.heartbeatTimer = invalid | |
end if | |
m.heartbeatTimer = CreateObject("roTimer") | |
m.heartbeatTimer.SetPort(m.msgPort) | |
m.heartbeatTimer.SetElapsed(heartBeatTimerDelaySec, 0) | |
heartbeatTimerAA = { name: "HeartbeatTimer" } | |
m.heartbeatTimer.SetUserData(heartbeatTimerAA) | |
m.heartbeatTimer.Start() | |
End Sub | |
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' | |
Sub CreateHeartbeatReceiver() | |
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' | |
m.heartbeatUdpReceivePort = heartBeatUdpPort | |
m.heartbeatUdpReceiver = CreateObject("roDatagramReceiver", m.heartbeatUdpReceivePort) | |
m.heartbeatUdpReceiver.SetPort(m.msgPort) | |
heartbeatUdpTimerAA = { name: "HeartbeatUdp" } | |
m.heartbeatUdpReceiver.SetUserData(heartbeatUdpTimerAA) | |
End Sub | |
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' | |
Sub CreateHtmlWidget() | |
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' | |
if m.htmlWidget <> invalid then | |
m.htmlWidget = invalid | |
end if | |
url$ = "file:///sd:/node-server.html" | |
htmlRect = CreateObject("roRectangle", 0, 0, 1920, 1080) | |
is = { port: 2999 } | |
config = { | |
nodejs_enabled: true | |
inspector_server: is | |
brightsign_js_objects_enabled: true | |
url: url$ | |
} | |
m.htmlWidget = CreateObject("roHtmlWidget", htmlRect, config) | |
m.htmlWidget.Show() | |
End Sub |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script src='./index.js'></script> | |
</head> | |
<body style='background-color:red' onload='window.main()'> | |
<h1> | |
Welcome to BrightSign! | |
</h1> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment