Best K6 code snippet using ws.Close
websocket.go
Source:websocket.go
...10 heartbeatIntervalTime time.Duration11 checkConnectIntervalTime time.Duration12 actived time.Time13 close chan int14 isClose bool15 subs []interface{}16}17const (18 SUB_TICKER = 1 + iota19 SUB_ORDERBOOK20 SUB_KLINE_1M21 SUB_KLINE_15M22 SUB_KLINE_30M23 SUB_KLINE_1D24 UNSUB_TICKER25 UNSUB_ORDERBOOK26)27func NewWsConn(wsurl string) *WsConn {28 wsConn, _, err := websocket.DefaultDialer.Dial(wsurl, nil)29 if err != nil {30 panic(err)31 }32 return &WsConn{Conn: wsConn, url: wsurl, actived: time.Now(), checkConnectIntervalTime: 30 * time.Second, close: make(chan int, 1)}33}34// ReConnect check the connect every checkConnectIntervalTime35func (ws *WsConn) ReConnect() {36 timer := time.NewTimer(ws.checkConnectIntervalTime)37 go func() {38 for {39 select {40 case <-timer.C:41 if time.Now().Sub(ws.actived) >= 2*ws.checkConnectIntervalTime {42 ws.Close()43 log.Println("start reconnect websocket:", ws.url)44 wsConn, _, err := websocket.DefaultDialer.Dial(ws.url, nil)45 if err != nil {46 log.Println("reconnect fail ???")47 } else {48 ws.Conn = wsConn49 ws.actived = time.Now()50 //re subscribe51 for _, sub := range ws.subs {52 log.Println("subscribe:", sub)53 ws.WriteJSON(sub)54 }55 }56 }57 timer.Reset(ws.checkConnectIntervalTime)58 case <-ws.close:59 timer.Stop()60 log.Println("close websocket connect, exiting reconnect goroutine.")61 return62 }63 }64 }()65}66// Heartbeat keep alive with server67func (ws *WsConn) Heartbeat(heartbeat func() interface{}, interval time.Duration) {68 ws.heartbeatIntervalTime = interval69 ws.checkConnectIntervalTime = 2 * ws.heartbeatIntervalTime70 timer := time.NewTimer(interval)71 go func() {72 for {73 select {74 case <-timer.C:75 err := ws.WriteJSON(heartbeat())76 if err != nil {77 log.Println("heartbeat error , ", err)78 time.Sleep(time.Second)79 }80 timer.Reset(interval)81 case <-ws.close:82 timer.Stop()83 log.Println("close websocket connect , exiting heartbeat goroutine.")84 return85 }86 }87 }()88}89// Subscribe subscribe some topic90func (ws *WsConn) Subscribe(subEvent interface{}) error {91 err := ws.WriteJSON(subEvent)92 if err != nil {93 return err94 }95 ws.subs = append(ws.subs, subEvent)96 return nil97}98// ReceiveMessage handle message from server99func (ws *WsConn) ReceiveMessage(handle func(msg []byte)) {100 go func() {101 for {102 t, msg, err := ws.ReadMessage()103 if err != nil {104 log.Println(err)105 if ws.isClose {106 log.Println("exiting receive message goroutine.")107 break108 }109 time.Sleep(time.Second)110 continue111 }112 switch t {113 case websocket.TextMessage, websocket.BinaryMessage:114 handle(msg)115 case websocket.PongMessage:116 ws.actived = time.Now()117 case websocket.CloseMessage:118 ws.CloseWs()119 return120 default:121 log.Println("error websocket message type , content is :\n", string(msg))122 }123 }124 }()125}126//UpdateActivedTime update websocket actived time127func (ws *WsConn) UpdateActivedTime() {128 ws.actived = time.Now()129}130// CloseWs close websocket131func (ws *WsConn) CloseWs() {132 ws.close <- 1 //exit reconnect goroutine133 if ws.heartbeatIntervalTime > 0 {134 ws.close <- 1 //exit heartbeat goroutine135 }136 err := ws.Close()137 if err != nil {138 log.Println("close websocket connect error , ", err)139 }140 ws.isClose = true141}...
ws_conn_x.go
Source:ws_conn_x.go
...33 wsConn.conn = conn34 return wsConn35}36func (wsConn *WSConn) doDestroy() {37 wsConn.conn.Close()38 if !wsConn.closeFlag {39 wsConn.closeFlag = true40 }41}42// Destroy 注éè¿æ¥43func (wsConn *WSConn) Destroy() {44 //wsConn.Lock()45 //defer wsConn.Unlock()46 wsConn.doDestroy()47}48// Close å
³éè¿æ¥49func (wsConn *WSConn) Close() error {50 //wsConn.Lock()51 //defer wsConn.Unlock()52 if wsConn.closeFlag {53 return nil54 }55 wsConn.closeFlag = true56 return wsConn.conn.Close()57}58// Write Write59func (wsConn *WSConn) Write(p []byte) (int, error) {60 return wsConn.conn.Write(p)61}62// Read goroutine not safe63func (wsConn *WSConn) Read(p []byte) (n int, err error) {64 return wsConn.conn.Read(p)65}66// LocalAddr è·åæ¬å°socketå°å67func (wsConn *WSConn) LocalAddr() net.Addr {68 return wsConn.conn.LocalAddr()69}70// RemoteAddr è·åè¿ç¨socketå°å...
Close
Using AI Code Generation
1ws.Close()2ws.Close()3ws.Close()4ws.Close()5ws.Close()6ws.Close()7ws.Close()8ws.Close()9ws.Close()10ws.Close()11ws.Close()12ws.Close()13ws.Close()14ws.Close()15ws.Close()16ws.Close()17ws.Close()
Close
Using AI Code Generation
1make( type, size )2import (3func main() {4 ch := make(chan int)5 fmt.Printf("%T", ch)6}7import (8func main() {9 ch := make(chan int)10 fmt.Println("Data sent to channel")11}12main.main()13import (14func main() {15 ch := make(chan int)16 fmt.Println("Data received from channel", data
Close
Using AI Code Generation
1cannot use ws (type *websocket.Conn) as type *websocket.Conn in argument to ws.Write2Your name to display (optional):3Your name to display (optional):4import (5func main() {6 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {7 ws, _ := websocket.Upgrade(w, r, w.Header(), 1024, 1024)8 ws.Close()9 ws.Write([]byte("Hello"))10 ws.Read()11 })12 http.ListenAndServe(":8080", nil)13}14Your name to display (optional):
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!