Update multichat example

This commit is contained in:
Ola
2022-11-26 13:56:28 +01:00
parent 36179d2ce2
commit c5e7d90715
2 changed files with 13 additions and 13 deletions
+1 -1
View File
@@ -21,7 +21,7 @@
</center>
<script>
var url = "ws://" + window.location.host + window.location.pathname + "/ws";
var url = "ws://" + window.location.host + "/ws";
var ws = new WebSocket(url);
var name = "Guest" + Math.floor(Math.random() * 1000);
var channelName = window.location.pathname.split("/")[2];
+12 -12
View File
@@ -1,25 +1,25 @@
package main
import (
"github.com/gin-gonic/gin"
"github.com/olahol/melody"
"net/http"
"github.com/olahol/melody"
)
func main() {
r := gin.Default()
m := melody.New()
r.GET("/", func(c *gin.Context) {
http.ServeFile(c.Writer, c.Request, "index.html")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
http.ServeFile(w, r, "index.html")
return
}
http.ServeFile(w, r, "chan.html")
})
r.GET("/channel/:name", func(c *gin.Context) {
http.ServeFile(c.Writer, c.Request, "chan.html")
})
r.GET("/channel/:name/ws", func(c *gin.Context) {
m.HandleRequest(c.Writer, c.Request)
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
m.HandleRequest(w, r)
})
m.HandleMessage(func(s *melody.Session, msg []byte) {
@@ -28,5 +28,5 @@ func main() {
})
})
r.Run(":5000")
http.ListenAndServe(":5000", nil)
}