multi rooms with some race for multi write to a ws

master
Bel LaPointe 2020-05-13 16:24:33 -06:00
parent 4145bea3ba
commit c9872c7725
3 changed files with 25 additions and 17 deletions

View File

@ -10,18 +10,16 @@
<div>Audio<label class="switch"><input onclick="toggle('audio', this)" type="checkbox"><span class="slider round"></span></label></div>
<video id="preview" autoplay muted ></video>
<div id="remote">
</div>
<div id="remote"></div>
<br />
<input type="button" id="start" onclick="start(true)" value="Start Video"></input>
<div id="log"></div>
</body>
<footer>
<script type="text/javascript">
pageReady();
</script>
<div id="log">
</div>
</body>
</footer>
</html>

View File

@ -25,14 +25,18 @@ class Config {
static getUUID() {
var uuid = Config.getCookie('uuid');
if (!uuid) {
uuid = Config.createUUID();
Config.setCookie('uuid', uuid);
Config.newUUID();
}
Log.info("uuid", uuid);
return uuid;
}
static newUUID() {
var uuid = Config.createUUID();
Config.setCookie('uuid', uuid);
}
static createUUID() {
return "" + new Date();
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
@ -88,6 +92,7 @@ class Preview {
class WS {
constructor(address, cb) {
Log.warn("address", address);
WS.ws = new WebSocket(address);
WS.ws.onmessage = cb;
}
@ -253,12 +258,12 @@ for (var i in ["log", "info", "warn", "error"]) {
window.console = console;
function pageReady() {
Config.getUUID();
Config.newUUID();
var host = window.location.hostname;
if (window.location.port) {
host += ":" + window.location.port;
}
new WS('wss://' + host + '/abc', Entropy.gotMessageFromServer);
new WS('wss://' + host + '/ws/' + window.location.pathname.split("/").slice(-1).pop(), Entropy.gotMessageFromServer);
Entropy.pageReady();
}

View File

@ -31,15 +31,20 @@ func New() *Server {
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Println("base", path.Base(r.URL.Path))
log.Println("dir", path.Dir(r.URL.Path))
if !s.Authorize(w, r) {
return
}
if path.Ext(r.URL.Path) != "" {
s.fs.ServeHTTP(w, r)
} else if _, err := os.Stat(path.Join(config().GetString("d"), r.URL.Path[1:])); os.IsNotExist(err) {
if path.Dir(r.URL.Path) == "/ws" {
s.ws.ServeHTTP(w, r)
} else {
s.fs.ServeHTTP(w, r)
r.URL.Path = "/" + path.Base(r.URL.Path)
if _, err := os.Stat(path.Join(config().GetString("d"), path.Base(r.URL.Path))); os.IsNotExist(err) {
r.URL.Path = "/"
s.fs.ServeHTTP(w, r)
} else {
s.fs.ServeHTTP(w, r)
}
}
}