Skip to content

Commit

Permalink
Fix web client connection, remove wsPath config
Browse files Browse the repository at this point in the history
  • Loading branch information
zedeus committed Feb 17, 2022
1 parent 76ba603 commit f7c3862
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion mpv_client.example.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ username = "guest"
password = "" # only used for admin authentication, leave blank if it's not your server

[Server]
address = "localhost:9001/ws" # "kinoplex.example.com/ws"
address = "localhost:9001" # e.g. kino.example.com or example.com/kino
useTLS = false # connect securely over wss instead of ws (requires HTTPS support)

[mpv]
Expand Down
1 change: 0 additions & 1 deletion server.example.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[Server]
port = 9001
staticDir = "./static"
wsPath = "/ws"
adminPassword = "1337"
pauseOnChange = true
pauseOnLeave = false
4 changes: 3 additions & 1 deletion src/kino_mpv.nim
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@ proc handleServer() {.async.} =
close server.ws

proc main() {.async.} =
server = Server(host: (if cfg.useTls: "wss://" else: "ws://") & cfg.address)
server = Server(host: (if cfg.useTls: "wss://" else: "ws://") & cfg.address & "/ws")
echo "Connecting to ", server.host

try:
server.ws = await newWebSocket(server.host)
player = await startMpv(cfg.binPath)
Expand Down
4 changes: 2 additions & 2 deletions src/kino_server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ proc serveFile(req: Request) {.async.} =
await req.respond(code, content)

proc cb(req: Request) {.async, gcsafe.} =
if req.url.path == cfg.wsPath:
if req.url.path == "/ws":
var client = Client()
try:
client.ws = await newWebSocket(req)
Expand All @@ -209,6 +209,6 @@ proc cb(req: Request) {.async, gcsafe.} =
else:
await serveFile(req)

echo "Listening at ws://localhost:", cfg.port, cfg.wsPath
echo "Listening at ws://localhost:", cfg.port, "/ws"
var server = newAsyncHttpServer()
waitFor server.serve(Port(cfg.port), cb)
26 changes: 21 additions & 5 deletions src/kino_web.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ type

var
player: Plyr
server = Server(host: "ws://localhost:9001/ws")
server: Server
name = $window.prompt("Enter username: ", "guest")
password = window.prompt("Enter password (or leave empty):", "")
role = user
authenticated = false
messages: seq[Msg]
activeTab: Tab
panel: Node
panel: Element
overlayActive = false
ovInputActive = false
overlayBox: Node
overlayBox: Element
timeout: TimeOut
hasPlayed: bool

Expand All @@ -48,6 +48,20 @@ proc handleInput()
proc send(s: Server; data: protocol.Event) =
server.ws.send(cstring($(%data)))

proc getServerUrl(): string =
let
protocol = $window.location.protocol
host = $window.location.host
path = $window.location.pathname

if protocol == "https:":
result = &"wss://{host}"
else:
result = &"ws://{host}"

if path == "/": result &= "/ws"
else: result &= &"{path}/ws"

proc switchTab(tab: Tab) =
activeTab = tab
let
Expand Down Expand Up @@ -310,7 +324,7 @@ proc playlistBox(): VNode =
for i, movie in server.playlist:
tdiv(class="movieElem"):
span(class="movieSource"):
a(href=kstring(movie)): text kstring(movie).split("://")[1]
a(href=kstring(movie)): text kstring($movie.split("://")[1])
if role == admin:
if server.index != i:
button(id="playMovie", index=i, class="actionBtn", onclick=parseAction):
Expand Down Expand Up @@ -374,7 +388,7 @@ proc init(p: var Plyr, id: string) =
p.on("playing", syncPlaying)
p.on("pause", syncPlaying)
document.addEventListener("keypress", onkeypress)

proc createDom(): VNode =
buildHtml(tdiv):
tdiv(id="kinopanel"):
Expand All @@ -400,5 +414,7 @@ proc postRender =
panel = document.getElementById("kinopanel")
scrollToBottom()

server = Server(host: getServerUrl())

setRenderer createDom, "ROOT", postRender
setForeignNodeId "player"
2 changes: 0 additions & 2 deletions src/server/config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ type
Config* = object
port*: int
staticDir*: string
wsPath*: string
adminPassword*: string
pauseOnChange*: bool
pauseOnLeave*: bool
Expand All @@ -15,7 +14,6 @@ proc getConfig*(): Config =
Config(
port: cfg.get("Server", "port", 9001),
staticDir: cfg.get("Server", "staticDir", "./static"),
wsPath: cfg.get("Server", "wsPath", "/ws"),
adminPassword: cfg.get("Server", "adminPassword", "1337"),
pauseOnChange: cfg.get("Server", "pauseOnChange", true),
pauseOnLeave: cfg.get("Server", "pauseOnLeave", false)
Expand Down

0 comments on commit f7c3862

Please sign in to comment.