Update multichat example
This commit is contained in:
@@ -21,7 +21,7 @@
|
|||||||
</center>
|
</center>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
var url = "ws://" + window.location.host + window.location.pathname + "/ws";
|
var url = "ws://" + window.location.host + "/ws";
|
||||||
var ws = new WebSocket(url);
|
var ws = new WebSocket(url);
|
||||||
var name = "Guest" + Math.floor(Math.random() * 1000);
|
var name = "Guest" + Math.floor(Math.random() * 1000);
|
||||||
var channelName = window.location.pathname.split("/")[2];
|
var channelName = window.location.pathname.split("/")[2];
|
||||||
|
|||||||
+12
-12
@@ -1,25 +1,25 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/olahol/melody"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/olahol/melody"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
r := gin.Default()
|
|
||||||
m := melody.New()
|
m := melody.New()
|
||||||
|
|
||||||
r.GET("/", func(c *gin.Context) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
http.ServeFile(c.Writer, c.Request, "index.html")
|
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.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
|
||||||
http.ServeFile(c.Writer, c.Request, "chan.html")
|
m.HandleRequest(w, r)
|
||||||
})
|
|
||||||
|
|
||||||
r.GET("/channel/:name/ws", func(c *gin.Context) {
|
|
||||||
m.HandleRequest(c.Writer, c.Request)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
m.HandleMessage(func(s *melody.Session, msg []byte) {
|
m.HandleMessage(func(s *melody.Session, msg []byte) {
|
||||||
@@ -28,5 +28,5 @@ func main() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Run(":5000")
|
http.ListenAndServe(":5000", nil)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user