基本完成
This commit is contained in:
parent
8b0cdeabb3
commit
07a3b90b3a
|
@ -1,6 +1,8 @@
|
||||||
package fusenrender
|
package fusenrender
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestMain(t *testing.T) {
|
func TestMain(t *testing.T) {
|
||||||
|
|
||||||
|
|
71
websocket.go
71
websocket.go
|
@ -9,6 +9,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/474420502/requests"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"github.com/lni/dragonboat/v4"
|
"github.com/lni/dragonboat/v4"
|
||||||
"github.com/lni/dragonboat/v4/client"
|
"github.com/lni/dragonboat/v4/client"
|
||||||
|
@ -23,9 +24,10 @@ var upgrader = websocket.Upgrader{
|
||||||
}
|
}
|
||||||
|
|
||||||
func HttpListen(ns *dragonboat.NodeHost, port int) {
|
func HttpListen(ns *dragonboat.NodeHost, port int) {
|
||||||
|
http.HandleFunc("/api/render/render_notify", callbackHandler)
|
||||||
http.HandleFunc("/api/queue/push", pushRenderTaskHandler)
|
http.HandleFunc("/api/queue/push", pushRenderTaskHandler)
|
||||||
http.HandleFunc("/ws/pop/queue", queueHandler)
|
http.HandleFunc("/ws/pop/queue", queueHandler)
|
||||||
http.HandleFunc("/ws/callback", callbackHandler)
|
|
||||||
log.Printf(":%d", port)
|
log.Printf(":%d", port)
|
||||||
http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
|
http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
|
||||||
}
|
}
|
||||||
|
@ -46,13 +48,6 @@ func queueHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
||||||
// msgType, msg, err := conn.ReadMessage()
|
|
||||||
|
|
||||||
// if err != nil {
|
|
||||||
// log.Println(msgType, msg, err)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
item := <-PopChannel
|
item := <-PopChannel
|
||||||
|
|
||||||
if item == nil {
|
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) {
|
func callbackHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
conn, _ := upgrader.Upgrade(w, r, nil)
|
var req RequestCallback
|
||||||
|
json.NewDecoder(r.Body).Decode(&req)
|
||||||
|
|
||||||
for {
|
urlstr := "http://localhost:9900/api/upload/upload-file-base"
|
||||||
// 读取消息
|
|
||||||
msgType, msg, err := conn.ReadMessage()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 打印消息
|
form := requests.M{}
|
||||||
fmt.Printf("%s sent: %s\n", conn.RemoteAddr(), string(msg))
|
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"] = ""
|
||||||
|
|
||||||
// 写回消息
|
log.Println(req)
|
||||||
err = conn.WriteMessage(msgType, msg)
|
|
||||||
if err != nil {
|
resp, err := requests.Post(urlstr).SetBodyFormData(form).Execute()
|
||||||
return
|
log.Println(resp.ContentString(), err)
|
||||||
}
|
|
||||||
}
|
// log.Println(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
func pushRenderTaskHandler(w http.ResponseWriter, r *http.Request) {
|
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) {
|
func TestWebsocketCient(t *testing.T) {
|
||||||
// u := url.URL{Scheme: "ws", Host: addr, Path: "/ws/pop/queue"}
|
// u := url.URL{Scheme: "ws", Host: addr, Path: "/ws/pop/queue"}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user