Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
f1729f129a | ||
|
f61302d25f | ||
|
b9d931da68 |
23
callback.go
Normal file
23
callback.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
package cwclient
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
// Callback 发送代理连接获取内容后的回调函数
|
||||
type Callback struct {
|
||||
label string
|
||||
hash string
|
||||
Do func(cxt *CallbackContext)
|
||||
}
|
||||
|
||||
// CallbackContext Callback上下文
|
||||
type CallbackContext struct {
|
||||
TaskID string
|
||||
Content string
|
||||
Error error
|
||||
Carry interface{} // 传递的参数.
|
||||
}
|
||||
|
||||
// ContentJSON 返回 json反序列化的对象
|
||||
func (cxt *CallbackContext) ContentJSON(obj interface{}) error {
|
||||
return json.Unmarshal([]byte(cxt.Content), obj)
|
||||
}
|
20
client.go
20
client.go
|
@ -21,21 +21,6 @@ func init() {
|
|||
log.SetFlags(log.Llongfile | log.LstdFlags)
|
||||
}
|
||||
|
||||
// CallbackContext Callback上下文
|
||||
type CallbackContext struct {
|
||||
TaskID string
|
||||
Content string
|
||||
Error error
|
||||
Carry interface{} // 传递的参数.
|
||||
}
|
||||
|
||||
// Callback 发送代理连接获取内容后的回调函数
|
||||
type Callback struct {
|
||||
label string
|
||||
hash string
|
||||
Do func(cxt *CallbackContext)
|
||||
}
|
||||
|
||||
// Client 客户端
|
||||
type Client struct {
|
||||
chromeProxyAddr string
|
||||
|
@ -183,7 +168,10 @@ func (cli *Client) Connect() {
|
|||
}
|
||||
cli.port = fmt.Sprintf("%d", listener.Addr().(*net.TCPAddr).Port)
|
||||
|
||||
router := gin.Default()
|
||||
router := gin.New()
|
||||
router.Use(gin.Recovery())
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
|
||||
router.POST("/:label", func(c *gin.Context) {
|
||||
label := c.Param("label")
|
||||
if f, ok := cli.register.Load(label); ok {
|
||||
|
|
|
@ -5,8 +5,30 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestSelectChan(t *testing.T) {
|
||||
var a chan string = make(chan string)
|
||||
var b chan string = make(chan string)
|
||||
|
||||
go func() {
|
||||
time.Sleep(time.Second * 2)
|
||||
a <- "1"
|
||||
time.Sleep(time.Second * 2)
|
||||
b <- "2"
|
||||
}()
|
||||
|
||||
select {
|
||||
case s := <-a:
|
||||
log.Println("a", s)
|
||||
case s := <-b:
|
||||
log.Println("b", s)
|
||||
case <-time.After(time.Second * 5):
|
||||
log.Println("timeout 5s")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPort(t *testing.T) {
|
||||
cli := New("http://localhost:7123")
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user