mirror of
https://github.com/supanadit/geo-smart-system.git
synced 2024-11-10 01:52:24 +00:00
Change to SSE from Socket IO
This commit is contained in:
parent
7673cf3425
commit
13ae28ff19
93
main.go
93
main.go
@ -1,93 +1,38 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"github.com/gin-contrib/cors"
|
"github.com/gin-contrib/cors"
|
||||||
|
"github.com/gin-contrib/sse"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/go-redis/redis"
|
"github.com/go-redis/redis"
|
||||||
socketio "github.com/googollee/go-socket.io"
|
|
||||||
"github.com/supanadit/geosmartsystem/model"
|
"github.com/supanadit/geosmartsystem/model"
|
||||||
"github.com/supanadit/geosmartsystem/model/tile38"
|
"github.com/supanadit/geosmartsystem/model/tile38"
|
||||||
"log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Tile38
|
|
||||||
client := redis.NewClient(&redis.Options{
|
client := redis.NewClient(&redis.Options{
|
||||||
Addr: "localhost:9851",
|
Addr: "localhost:9851",
|
||||||
})
|
})
|
||||||
|
r := gin.Default()
|
||||||
router := gin.Default()
|
r.Use(cors.Default())
|
||||||
//router.Use(cors.New(cors.Config{
|
r.POST("/set-points", func(c *gin.Context) {
|
||||||
// AllowOrigins: []string{"http://localhost:4200"},
|
|
||||||
// AllowMethods: []string{"PUT", "PATCH", "POST", "GET"},
|
|
||||||
// AllowHeaders: []string{"Origin"},
|
|
||||||
// ExposeHeaders: []string{"Content-Length"},
|
|
||||||
// AllowWebSockets: true,
|
|
||||||
// AllowCredentials: true,
|
|
||||||
// AllowWildcard: true,
|
|
||||||
//}))
|
|
||||||
router.Use(cors.Default())
|
|
||||||
router.GET("/points", func(c *gin.Context) {
|
|
||||||
data, _ := tile38.FromScan(client, "sales")
|
|
||||||
c.JSON(200, data)
|
|
||||||
})
|
|
||||||
// Socket.IO Start
|
|
||||||
server, err := socketio.NewServer(nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
server.OnConnect("/", func(s socketio.Conn) error {
|
|
||||||
s.SetContext("")
|
|
||||||
fmt.Println("Connected:", s.ID())
|
|
||||||
data, _ := tile38.FromScan(client, "sales")
|
|
||||||
s.Emit("points", data)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
server.OnEvent("/", "set-points", func(s socketio.Conn, msg string) {
|
|
||||||
var location model.Location
|
var location model.Location
|
||||||
err = json.Unmarshal([]byte(msg), &location)
|
_ = c.BindJSON(&location)
|
||||||
if err != nil {
|
c.Writer.Header().Set("Content-Type", "application/json")
|
||||||
fmt.Println(err)
|
client.Do("SET", location.Type, location.Id, "POINT", location.Lat, location.Lng)
|
||||||
} else {
|
c.JSON(200, gin.H{"status": "ok"})
|
||||||
fmt.Println(location)
|
|
||||||
data, err := tile38.GetDataLocation(client, "sales", location.Id)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
} else {
|
|
||||||
fmt.Println(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
server.OnEvent("/", "bye", func(s socketio.Conn) string {
|
r.GET("/stream", func(c *gin.Context) {
|
||||||
last := s.Context().(string)
|
writer := c.Writer
|
||||||
s.Emit("bye", last)
|
writer.Header().Set("Content-Type", "text/event-stream")
|
||||||
_ = s.Close()
|
writer.Header().Set("Cache-Control", "no-cache")
|
||||||
return last
|
writer.Header().Set("Connection", "keep-alive")
|
||||||
|
data, _ := tile38.FromScan(client, "sales")
|
||||||
|
_ = sse.Encode(writer, sse.Event{
|
||||||
|
Event: "message",
|
||||||
|
Data: data,
|
||||||
})
|
})
|
||||||
server.OnError("/", func(e error) {
|
|
||||||
fmt.Println("Meet Error:", e)
|
|
||||||
})
|
})
|
||||||
server.OnDisconnect("/", func(s socketio.Conn, msg string) {
|
r.Static("/public", "./public")
|
||||||
fmt.Println("Closed", msg)
|
_ = r.Run()
|
||||||
})
|
|
||||||
router.GET("/socket.io/", gin.WrapH(server))
|
|
||||||
router.POST("/socket.io/", gin.WrapH(server))
|
|
||||||
router.Handle("WS", "/socket.io/", WebSocketIO(server))
|
|
||||||
router.Handle("WSS", "/socket.io/", WebSocketIO(server))
|
|
||||||
router.GET("/ws", func(c *gin.Context) {
|
|
||||||
server.ServeHTTP(c.Writer, c.Request)
|
|
||||||
})
|
|
||||||
go server.Serve()
|
|
||||||
defer server.Close()
|
|
||||||
// End Socket.IO
|
|
||||||
_ = router.Run()
|
|
||||||
}
|
|
||||||
|
|
||||||
func WebSocketIO(server *socketio.Server) gin.HandlerFunc {
|
|
||||||
return func(c *gin.Context) {
|
|
||||||
server.ServeHTTP(c.Writer, c.Request)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
17
public/index.html
Normal file
17
public/index.html
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title></title>
|
||||||
|
</head>
|
||||||
|
<p id="test"></p>
|
||||||
|
<body>
|
||||||
|
<script>
|
||||||
|
const id = document.getElementById("test");
|
||||||
|
const stream = new EventSource("/stream");
|
||||||
|
stream.onmessage = function (event) {
|
||||||
|
id.innerText = event.data
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue
Block a user