@@ -5,10 +5,11 @@ import (
55 "encoding/json"
66 "errors"
77 "fmt"
8+ "strings"
9+
810 "github.com/Authing/authing-golang-sdk/v3/constant"
911 "github.com/Authing/authing-golang-sdk/v3/dto"
1012 "github.com/Authing/authing-golang-sdk/v3/util"
11- "strings"
1213
1314 keyfunc "github.com/MicahParks/compatibility-keyfunc"
1415 "github.com/dgrijalva/jwt-go"
@@ -25,8 +26,9 @@ var commonHeaders = map[string]string{
2526}
2627
2728type AuthenticationClient struct {
28- options * AuthenticationClientOptions
29- jwks * keyfunc.JWKS
29+ options * AuthenticationClientOptions
30+ jwks * keyfunc.JWKS
31+ eventHub * util.WebSocketEventHub
3032}
3133
3234func NewAuthenticationClient (options * AuthenticationClientOptions ) (* AuthenticationClient , error ) {
@@ -39,6 +41,9 @@ func NewAuthenticationClient(options *AuthenticationClientOptions) (*Authenticat
3941 if options .AppHost == "" {
4042 return nil , errors .New ("AppHost 不能为空" )
4143 }
44+ if options .WssHost == "" {
45+ options .WssHost = constant .WebSocketHost
46+ }
4247 if options .RedirectUri == "" {
4348 return nil , errors .New ("RedirectUri 不能为空" )
4449 }
@@ -56,7 +61,8 @@ func NewAuthenticationClient(options *AuthenticationClientOptions) (*Authenticat
5661 }
5762
5863 client := & AuthenticationClient {
59- options : options ,
64+ options : options ,
65+ eventHub : util .NewWebSocketEvent (),
6066 }
6167
6268 return client , nil
@@ -2204,3 +2210,55 @@ func (client *AuthenticationClient) getUserAuthResourceStruct(reqDto *dto.GetUse
22042210 }
22052211 return & response
22062212}
2213+
2214+ /*
2215+ * @summary 事件发布
2216+ * @description 根据事件编码发布一个自定义事件
2217+ * @param eventCode 事件编码
2218+ * @param body 事件消息
2219+ * @returns IsSuccessRespDto
2220+ */
2221+ func (client * AuthenticationClient ) PubEvent (eventCode string , data interface {}) * dto.IsSuccessRespDto {
2222+ var reqDto = dto .NewEventReqDto (eventCode , data )
2223+ b , err := client .SendHttpRequest ("/api/v3/pub-userEvent" , fasthttp .MethodPost , reqDto )
2224+ var response dto.IsSuccessRespDto
2225+ if err != nil {
2226+ fmt .Println (err )
2227+ return nil
2228+ }
2229+ err = json .Unmarshal (b , & response )
2230+ if err != nil {
2231+ fmt .Println (err )
2232+ return nil
2233+ }
2234+ return & response
2235+ }
2236+
2237+ /*
2238+ * @summary 事件订阅
2239+ * @description 根据事件编码订阅一个自定义事件
2240+ * @param eventCode 事件编码
2241+ * @param onSuccess 成功的消息
2242+ * @param onError 异常处理
2243+ */
2244+ func (client * AuthenticationClient ) SubEvent (eventCode string , onSuccess func (msg []byte ), onError func (err error )) {
2245+ var options = client .options
2246+ token := options .AccessToken
2247+ // fmt.Println(token)
2248+ if ! client .eventHub .CreateAuthentication (eventCode , options .WssHost , token ) {
2249+ return
2250+ }
2251+ client .eventHub .AddReceiver (eventCode , onSuccess , onError )
2252+ // recv message exec corresponding callback function
2253+ go client .eventHub .StartReceive (eventCode )
2254+ }
2255+
2256+ /*
2257+ * @summary 事件订阅
2258+ * @description 根据事件编码订阅一个自定义事件
2259+ * @param eventCode 事件编码
2260+ * @param receiver 消息处理器
2261+ */
2262+ func (client * AuthenticationClient ) SubEventByReceiver (eventCode string , receiver util.EventReceiver ) {
2263+ client .SubEvent (eventCode , receiver .OnSuccess , receiver .OnError )
2264+ }
0 commit comments