2019-10-19 16:38:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-10-20 03:34:17 +00:00
|
|
|
"encoding/json"
|
2019-10-19 16:38:38 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/gin-contrib/cors"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/go-redis/redis"
|
|
|
|
socketio "github.com/googollee/go-socket.io"
|
|
|
|
"github.com/supanadit/geosmartsystem/model"
|
2019-10-20 03:34:17 +00:00
|
|
|
"github.com/supanadit/geosmartsystem/model/tile38"
|
2019-10-19 16:38:38 +00:00
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Tile38
|
|
|
|
client := redis.NewClient(&redis.Options{
|
|
|
|
Addr: "localhost:9851",
|
|
|
|
})
|
|
|
|
|
|
|
|
router := gin.Default()
|
2019-10-20 03:34:17 +00:00
|
|
|
//router.Use(cors.New(cors.Config{
|
|
|
|
// 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")
|
2019-10-19 16:38:38 +00:00
|
|
|
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())
|
2019-10-20 03:34:17 +00:00
|
|
|
data, _ := tile38.FromScan(client, "sales")
|
|
|
|
s.Emit("points", data)
|
2019-10-19 16:38:38 +00:00
|
|
|
return nil
|
|
|
|
})
|
2019-10-20 03:34:17 +00:00
|
|
|
server.OnEvent("/", "set-points", func(s socketio.Conn, msg string) {
|
|
|
|
var location model.Location
|
|
|
|
err = json.Unmarshal([]byte(msg), &location)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
|
|
|
fmt.Println(location)
|
|
|
|
data, err := tile38.GetDataLocation(client, "sales", location.Id)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
|
|
|
fmt.Println(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-10-19 16:38:38 +00:00
|
|
|
})
|
|
|
|
server.OnEvent("/", "bye", func(s socketio.Conn) string {
|
|
|
|
last := s.Context().(string)
|
|
|
|
s.Emit("bye", last)
|
|
|
|
_ = s.Close()
|
|
|
|
return last
|
|
|
|
})
|
|
|
|
server.OnError("/", func(e error) {
|
|
|
|
fmt.Println("Meet Error:", e)
|
|
|
|
})
|
|
|
|
server.OnDisconnect("/", func(s socketio.Conn, msg string) {
|
|
|
|
fmt.Println("Closed", msg)
|
|
|
|
})
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|