-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathconn.go
120 lines (99 loc) · 2.66 KB
/
conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Supports Windows, Linux, Mac, and Raspberry Pi
package main
import (
"net/http"
"strconv"
log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/googollee/go-socket.io"
)
type connection struct {
// The websocket connection.
ws socketio.Socket
// Buffered channel of outbound messages.
send chan []byte
incoming chan []byte
}
func (c *connection) writer() {
for message := range c.send {
err := c.ws.Emit("message", string(message))
if err != nil {
break
}
}
}
// WsServer overrides socket.io server to set the CORS
type WsServer struct {
Server *socketio.Server
}
func (s *WsServer) ServeHTTP(c *gin.Context) {
s.Server.ServeHTTP(c.Writer, c.Request)
}
func uploadHandler(c *gin.Context) {
log.Print("Received a upload")
port := c.PostForm("port")
if port == "" {
c.String(http.StatusBadRequest, "port is required")
return
}
board := c.PostForm("board")
if board == "" {
c.String(http.StatusBadRequest, "board is required")
log.Error("board is required")
return
}
board_rewrite := c.PostForm("board_rewrite")
var extraInfo boardExtraInfo
extraInfo.authdata.UserName = c.PostForm("auth_user")
extraInfo.authdata.Password = c.PostForm("auth_pass")
commandline := c.PostForm("commandline")
if commandline == "undefined" {
commandline = ""
}
extraInfo.use_1200bps_touch, _ = strconv.ParseBool(c.PostForm("use_1200bps_touch"))
extraInfo.wait_for_upload_port, _ = strconv.ParseBool(c.PostForm("wait_for_upload_port"))
extraInfo.networkPort, _ = strconv.ParseBool(c.PostForm("network"))
if extraInfo.networkPort == false && commandline == "" {
c.String(http.StatusBadRequest, "commandline is required for local board")
log.Error("commandline is required for local board")
return
}
sketch, header, err := c.Request.FormFile("sketch_hex")
if err != nil {
c.String(http.StatusBadRequest, err.Error())
}
if header != nil {
path, err := saveFileonTempDir(header.Filename, sketch)
if err != nil {
c.String(http.StatusBadRequest, err.Error())
}
if board_rewrite != "" {
board = board_rewrite
}
go spProgramRW(port, board, path, commandline, extraInfo)
}
}
func wsHandler() *WsServer {
server, err := socketio.NewServer(nil)
if err != nil {
log.Fatal(err)
}
server.On("connection", func(so socketio.Socket) {
c := &connection{send: make(chan []byte, 256*10), ws: so}
h.register <- c
so.On("command", func(message string) {
h.broadcast <- []byte(message)
})
so.On("disconnection", func() {
h.unregister <- c
})
go c.writer()
})
server.On("error", func(so socketio.Socket, err error) {
log.Println("error:", err)
})
wrapper := WsServer{
Server: server,
}
return &wrapper
}