-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
177 additions
and
74 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
export function streamEvents( | ||
path: string, | ||
callback: (data: string) => void | ||
callbacks: { [key: string]: (data: string) => void } | ||
): void { | ||
const es = new EventSource(path); | ||
es.addEventListener("play-sound", (event) => { | ||
callback(event.data); | ||
}); | ||
|
||
for (const eventType in callbacks) { | ||
if (callbacks.hasOwnProperty(eventType)) { | ||
es.addEventListener(eventType, (event: MessageEvent) => { | ||
callbacks[eventType](event.data); | ||
}); | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,40 @@ | ||
{{ define "sound-events" }} | ||
|
||
<script type="module"> | ||
import {getEnv} from "/z2/env.js"; | ||
|
||
const bluetoothConnectedHandler = (() => { | ||
const seen = {}; | ||
|
||
return (data) => { | ||
const msg = JSON.parse(data); | ||
const key = msg.address; | ||
if (key === "") { | ||
return; | ||
} | ||
if (seen[key]) { | ||
return; | ||
} | ||
seen[key] = true; | ||
|
||
const kind = msg.kind; | ||
const name = msg.name ? " " + msg.name : ""; | ||
|
||
const info = `${kind}${name} is connected`; | ||
console.log(info); | ||
}; | ||
})(); | ||
|
||
document.addEventListener("DOMContentLoaded", async () => { | ||
const env = await getEnv(); | ||
env.streamEvents("/events", data => { | ||
const audio = document.getElementById(`sound-${data}`); | ||
audio.play(); | ||
|
||
// Use the `bluetoothConnectedHandler` in `streamEvents` | ||
env.streamEvents("/events", { | ||
"play-sound": (data) => { | ||
const audio = document.getElementById(`sound-${data}`); | ||
if (audio) { | ||
audio.play(); | ||
} | ||
}, | ||
"bluetooth-is-connected": bluetoothConnectedHandler | ||
}); | ||
}); | ||
</script> | ||
|
||
{{ end }} |
This file contains 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
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func sse( | ||
router *gin.Engine, | ||
path string, | ||
handler func( | ||
c *gin.Context, | ||
send func(eventName, data string) error, | ||
), | ||
) { | ||
router.GET(path, func(c *gin.Context) { | ||
c.Writer.Header().Set("Content-Type", "text/event-stream") | ||
c.Writer.Header().Set("Cache-Control", "no-cache") | ||
c.Writer.Header().Set("Connection", "keep-alive") | ||
|
||
send := func(eventName, data string) error { | ||
if eventName != "" { | ||
_, err := c.Writer.Write([]byte("event: " + eventName + "\n")) | ||
if err != nil { | ||
return errors.Wrap(err, "write event") | ||
} | ||
} | ||
|
||
_, err := c.Writer.Write([]byte("data: " + data + "\n\n")) | ||
if err != nil { | ||
if err != nil { | ||
return errors.Wrap(err, "write data") | ||
} | ||
} | ||
c.Writer.Flush() | ||
return nil | ||
} | ||
|
||
handler(c, send) | ||
}) | ||
} |