基本完成
This commit is contained in:
parent
8b0cdeabb3
commit
07a3b90b3a
|
@ -1,6 +1,8 @@
|
|||
package fusenrender
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMain(t *testing.T) {
|
||||
|
||||
|
|
71
websocket.go
71
websocket.go
|
@ -9,6 +9,7 @@ import (
|
|||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/474420502/requests"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/lni/dragonboat/v4"
|
||||
"github.com/lni/dragonboat/v4/client"
|
||||
|
@ -23,9 +24,10 @@ var upgrader = websocket.Upgrader{
|
|||
}
|
||||
|
||||
func HttpListen(ns *dragonboat.NodeHost, port int) {
|
||||
http.HandleFunc("/api/render/render_notify", callbackHandler)
|
||||
http.HandleFunc("/api/queue/push", pushRenderTaskHandler)
|
||||
http.HandleFunc("/ws/pop/queue", queueHandler)
|
||||
http.HandleFunc("/ws/callback", callbackHandler)
|
||||
|
||||
log.Printf(":%d", port)
|
||||
http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
|
||||
}
|
||||
|
@ -46,13 +48,6 @@ func queueHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
for {
|
||||
|
||||
// msgType, msg, err := conn.ReadMessage()
|
||||
|
||||
// if err != nil {
|
||||
// log.Println(msgType, msg, err)
|
||||
// return
|
||||
// }
|
||||
|
||||
item := <-PopChannel
|
||||
|
||||
if item == nil {
|
||||
|
@ -81,25 +76,55 @@ func queueHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
type RenderCallback struct {
|
||||
// 定义表单结构体
|
||||
|
||||
}
|
||||
|
||||
type Form struct {
|
||||
UserID int `json:"user_id"`
|
||||
GuestID int `json:"guest_id"`
|
||||
APIType int `json:"api_type"`
|
||||
UploadBucket int `json:"upload_bucket"`
|
||||
FileKey string `json:"file_key"`
|
||||
FileData []byte `json:"file_data"`
|
||||
MetaData string `json:"meta_data"`
|
||||
}
|
||||
|
||||
// RequestCallback结构体
|
||||
type RequestCallback struct {
|
||||
Sign string `json:"sign"`
|
||||
Time int64 `json:"time"`
|
||||
Info Info `json:"info"`
|
||||
}
|
||||
|
||||
// Info结构体
|
||||
type Info struct {
|
||||
TaskID string `json:"task_id"`
|
||||
Image string `json:"image"`
|
||||
}
|
||||
|
||||
func callbackHandler(w http.ResponseWriter, r *http.Request) {
|
||||
conn, _ := upgrader.Upgrade(w, r, nil)
|
||||
var req RequestCallback
|
||||
json.NewDecoder(r.Body).Decode(&req)
|
||||
|
||||
for {
|
||||
// 读取消息
|
||||
msgType, msg, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
urlstr := "http://localhost:9900/api/upload/upload-file-base"
|
||||
|
||||
// 打印消息
|
||||
fmt.Printf("%s sent: %s\n", conn.RemoteAddr(), string(msg))
|
||||
form := requests.M{}
|
||||
form["user_id"] = 0
|
||||
form["guest_id"] = 0
|
||||
form["api_type"] = 2
|
||||
form["upload_bucket"] = 2
|
||||
form["file_key"] = req.Info.TaskID
|
||||
form["file_data"] = req.Info.Image
|
||||
form["meta_data"] = ""
|
||||
|
||||
// 写回消息
|
||||
err = conn.WriteMessage(msgType, msg)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
log.Println(req)
|
||||
|
||||
resp, err := requests.Post(urlstr).SetBodyFormData(form).Execute()
|
||||
log.Println(resp.ContentString(), err)
|
||||
|
||||
// log.Println(req)
|
||||
}
|
||||
|
||||
func pushRenderTaskHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
@ -42,7 +42,7 @@ func TestWebsocketC(t *testing.T) {
|
|||
|
||||
}
|
||||
|
||||
var addr = "localhost:4052"
|
||||
var addr = "localhost:4050"
|
||||
|
||||
func TestWebsocketCient(t *testing.T) {
|
||||
// u := url.URL{Scheme: "ws", Host: addr, Path: "/ws/pop/queue"}
|
||||
|
|
Loading…
Reference in New Issue
Block a user