Add standard library example
This commit is contained in:
@@ -0,0 +1,50 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Melody example: chatting</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
#chat {
|
||||||
|
text-align: left;
|
||||||
|
background: #f1f1f1;
|
||||||
|
width: 500px;
|
||||||
|
min-height: 300px;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<center>
|
||||||
|
<h3>Chat</h3>
|
||||||
|
<pre id="chat"></pre>
|
||||||
|
<input placeholder="say something" id="text" type="text">
|
||||||
|
</center>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
var url = "ws://" + window.location.host + "/ws";
|
||||||
|
var ws = new WebSocket(url);
|
||||||
|
var name = "Guest" + Math.floor(Math.random() * 1000);
|
||||||
|
|
||||||
|
var chat = document.getElementById("chat");
|
||||||
|
var text = document.getElementById("text");
|
||||||
|
|
||||||
|
var now = function () {
|
||||||
|
var iso = new Date().toISOString();
|
||||||
|
return iso.split("T")[1].split(".")[0];
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onmessage = function (msg) {
|
||||||
|
var line = now() + " " + msg.data + "\n";
|
||||||
|
chat.innerText += line;
|
||||||
|
};
|
||||||
|
|
||||||
|
text.onkeydown = function (e) {
|
||||||
|
if (e.keyCode === 13 && text.value !== "") {
|
||||||
|
ws.send("<" + name + "> " + text.value);
|
||||||
|
text.value = "";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/olahol/melody"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := gin.Default()
|
||||||
|
m := melody.New()
|
||||||
|
|
||||||
|
r.GET("/", func(c *gin.Context) {
|
||||||
|
http.ServeFile(c.Writer, c.Request, "index.html")
|
||||||
|
})
|
||||||
|
|
||||||
|
r.GET("/ws", func(c *gin.Context) {
|
||||||
|
m.HandleRequest(c.Writer, c.Request)
|
||||||
|
})
|
||||||
|
|
||||||
|
m.HandleMessage(func(s *melody.Session, msg []byte) {
|
||||||
|
m.Broadcast(msg)
|
||||||
|
})
|
||||||
|
|
||||||
|
r.Run(":5000")
|
||||||
|
}
|
||||||
@@ -1,26 +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")
|
http.ServeFile(w, r, "index.html")
|
||||||
})
|
})
|
||||||
|
|
||||||
r.GET("/ws", func(c *gin.Context) {
|
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
|
||||||
m.HandleRequest(c.Writer, c.Request)
|
m.HandleRequest(w, r)
|
||||||
})
|
})
|
||||||
|
|
||||||
m.HandleMessage(func(s *melody.Session, msg []byte) {
|
m.HandleMessage(func(s *melody.Session, msg []byte) {
|
||||||
m.Broadcast(msg)
|
m.Broadcast(msg)
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Run(":5000")
|
http.ListenAndServe(":5000", nil)
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 149 KiB |
Reference in New Issue
Block a user