Embed pages and use wss: under https:
This commit is contained in:
@@ -181,8 +181,12 @@
|
||||
warnDiv.style.display = "block";
|
||||
}
|
||||
|
||||
var proto = "ws:";
|
||||
if (location.protocol == "https:")
|
||||
proto = "wss:";
|
||||
|
||||
showStatus("Connecting...");
|
||||
const conn = new WebSocket("ws://" + document.location.host + "/host/ws");
|
||||
const conn = new WebSocket(`${proto}//${location.host}/host/ws`);
|
||||
conn.addEventListener("close", e => { showStatus("Disconnected."); });
|
||||
conn.addEventListener("error", e => { showError("Websocket error."); });
|
||||
conn.addEventListener("message", e => { handleMessage(e.data, conn); });
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
|
||||
<main>
|
||||
<h3>Join a Team</h3>
|
||||
<form action="/player">
|
||||
<form action="/player.html">
|
||||
<input type="hidden" name="token" id="token"/>
|
||||
|
||||
<div>
|
||||
|
||||
25
main.go
25
main.go
@@ -1,37 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"flag"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var addr = flag.String("addr", ":8080", "http server address")
|
||||
var host = flag.String("host", "localhost", "http server canonical hostname")
|
||||
//go:embed *.html
|
||||
var pages embed.FS
|
||||
|
||||
func servePages(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/":
|
||||
http.ServeFile(w, r, "home.html")
|
||||
case "/host":
|
||||
http.ServeFile(w, r, "host.html")
|
||||
case "/player":
|
||||
http.ServeFile(w, r, "player.html")
|
||||
default:
|
||||
http.Error(w, "Not found", http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
var addr = flag.String("listen", ":8080", "http server listen address")
|
||||
var host = flag.String("host", "localhost", "public http server canonical hostname")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
game := newGame()
|
||||
|
||||
log.Printf("Player link: http://%s%s/?token=%s", *host, *addr, tokenize(game.playerToken))
|
||||
log.Printf("Host link: http://%s%s/host?token=%s", *host, *addr, tokenize(game.hostToken))
|
||||
log.Printf("Player link: http://%s/?token=%s", *host, tokenize(game.playerToken))
|
||||
log.Printf("Host link: http://%s/host.html?token=%s", *host, tokenize(game.hostToken))
|
||||
|
||||
go game.run()
|
||||
|
||||
http.HandleFunc("/", servePages)
|
||||
http.Handle("/", http.FileServer(http.FS(pages)))
|
||||
http.HandleFunc("/player/ws", func(w http.ResponseWriter, r *http.Request) {
|
||||
servePlayerWs(game, w, r)
|
||||
})
|
||||
|
||||
@@ -54,7 +54,7 @@ func (p *Player) activate(story string) {
|
||||
game := p.Client.game
|
||||
game.activePlayer = p
|
||||
|
||||
log.Printf("Activating player %s", p.Name)
|
||||
log.Printf("Card for player %s: %s", p.Name, story)
|
||||
msg := makeMessage("card", story)
|
||||
p.send(msg)
|
||||
}
|
||||
|
||||
@@ -128,8 +128,12 @@
|
||||
warnDiv.style.display = "block";
|
||||
}
|
||||
|
||||
var proto = "ws:";
|
||||
if (location.protocol == "https:")
|
||||
proto = "wss:";
|
||||
|
||||
showStatus("Connecting...");
|
||||
const conn = new WebSocket("ws://" + document.location.host + "/player/ws");
|
||||
const conn = new WebSocket(`${proto}//${location.host}/player/ws`);
|
||||
conn.addEventListener("close", e => { showStatus("Disconnected."); });
|
||||
conn.addEventListener("error", e => { showError("Websocket error."); });
|
||||
conn.addEventListener("message", e => { handleMessage(e.data, conn); });
|
||||
|
||||
Reference in New Issue
Block a user