github.com/godaddy-x/freego@v1.0.156/ws_client_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "github.com/godaddy-x/freego/node" 6 "github.com/godaddy-x/freego/utils" 7 "github.com/godaddy-x/freego/utils/jwt" 8 "testing" 9 ) 10 11 func newClient() { 12 13 //rand.Seed(time.Now().UnixNano()) 14 //randomNumber := rand.Intn(3000) // 生成0到9的随机整数 15 //if randomNumber < 100 { 16 // randomNumber = randomNumber + 100 17 //} 18 //time.Sleep(time.Duration(randomNumber) * time.Millisecond) 19 20 authCall := func() (string, string, error) { 21 config := jwt.JwtConfig{ 22 TokenTyp: jwt.JWT, 23 TokenAlg: jwt.HS256, 24 TokenKey: "123456" + utils.CreateLocalSecretKey(12, 45, 23, 60, 58, 30), 25 TokenExp: jwt.TWO_WEEK, 26 } 27 subject := jwt.Subject{} 28 jwtToken := subject.Create(utils.NextSID()).Dev("APP").Generate(config) 29 jwtSecret := jwt.GetTokenSecret(jwtToken, config.TokenKey) 30 //jwtToken := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxNzU2NTEwOTIwMzAyOTE5NjgxIiwiYXVkIjoiIiwiaXNzIjoiIiwiaWF0IjowLCJleHAiOjE3MDg4Mjk0MTIsImRldiI6IkFQUCIsImp0aSI6InEyTEtYTG1uYXJ5MGhlM0FmVE9ZcFE9PSIsImV4dCI6IiJ9.T5o3LYyncHp2H7yWhj1S+MoWCj68KrcjfpZzqf0qxL8=" 31 //jwtSecret := "liEnMu77ysCaU4BHy*kT^j#lKWM1JqTPhjSHNtEl#lK!ZC@diQRl04GSsDVIQnU=" 32 return jwtToken, jwtSecret, nil 33 } 34 35 receiveCall := func(message []byte) (interface{}, error) { 36 reply := MsgReply{} 37 if err := utils.JsonUnmarshal(message, &reply); err != nil { 38 return nil, err 39 } 40 fmt.Println("receive:", reply) 41 reply.Ack = true 42 return &reply, nil 43 } 44 45 client := node.WsClient{ 46 Addr: "ws://localhost:6060", 47 Path: "/websocket", 48 Origin: "*", 49 AuthCall: authCall, 50 ReceiveCall: receiveCall, 51 } 52 //go func() { 53 // for { 54 // a := map[string]interface{}{"type": "test", "aaaaaaa": utils.NextSID()} 55 // client.SendMessage(&a) 56 // time.Sleep(5 * time.Second) 57 // } 58 //}() 59 client.StartWebsocket(true) 60 } 61 62 func TestWsClient(t *testing.T) { 63 for i := 0; i < 50; i++ { 64 go newClient() 65 } 66 select {} 67 }