How to use trackPong method of ws Package

Best K6 code snippet using ws.trackPong

ws.go

Source:ws.go Github

copy

Full Screen

...219 }220 socket.handleEvent("ping")221 case pingID := <-pongChan:222 // Handle pong responses to our pings223 socket.trackPong(pingID)224 socket.handleEvent("pong")225 case readData := <-readDataChan:226 socket.msgReceivedTimestamps = append(socket.msgReceivedTimestamps, time.Now())227 socket.handleEvent("message", rt.ToValue(string(readData)))228 case readErr := <-readErrChan:229 socket.handleEvent("error", rt.ToValue(readErr))230 case code := <-readCloseChan:231 _ = socket.closeConnection(code)232 case scheduledFn := <-socket.scheduled:233 if _, err := scheduledFn(goja.Undefined()); err != nil {234 return nil, err235 }236 case <-ctx.Done():237 // VU is shutting down during an interrupt238 // socket events will not be forwarded to the VU239 _ = socket.closeConnection(websocket.CloseGoingAway)240 case <-socket.done:241 // This is the final exit point normally triggered by closeConnection242 end := time.Now()243 sessionDuration := stats.D(end.Sub(start))244 sampleTags := stats.IntoSampleTags(&tags)245 stats.PushIfNotCancelled(ctx, state.Samples, stats.ConnectedSamples{246 Samples: []stats.Sample{247 {Metric: metrics.WSSessions, Time: start, Tags: sampleTags, Value: 1},248 {Metric: metrics.WSConnecting, Time: start, Tags: sampleTags, Value: connectionDuration},249 {Metric: metrics.WSSessionDuration, Time: start, Tags: sampleTags, Value: sessionDuration},250 },251 Tags: sampleTags,252 Time: start,253 })254 for _, msgSentTimestamp := range socket.msgSentTimestamps {255 stats.PushIfNotCancelled(ctx, state.Samples, stats.Sample{256 Metric: metrics.WSMessagesSent,257 Time: msgSentTimestamp,258 Tags: sampleTags,259 Value: 1,260 })261 }262 for _, msgReceivedTimestamp := range socket.msgReceivedTimestamps {263 stats.PushIfNotCancelled(ctx, state.Samples, stats.Sample{264 Metric: metrics.WSMessagesReceived,265 Time: msgReceivedTimestamp,266 Tags: sampleTags,267 Value: 1,268 })269 }270 for _, pingDelta := range socket.pingTimestamps {271 stats.PushIfNotCancelled(ctx, state.Samples, stats.Sample{272 Metric: metrics.WSPing,273 Time: pingDelta.pong,274 Tags: sampleTags,275 Value: stats.D(pingDelta.pong.Sub(pingDelta.ping)),276 })277 }278 return wsResponse, nil279 }280 }281}282func (s *Socket) On(event string, handler goja.Value) {283 if handler, ok := goja.AssertFunction(handler); ok {284 s.eventHandlers[event] = append(s.eventHandlers[event], handler)285 }286}287func (s *Socket) handleEvent(event string, args ...goja.Value) {288 if handlers, ok := s.eventHandlers[event]; ok {289 for _, handler := range handlers {290 if _, err := handler(goja.Undefined(), args...); err != nil {291 common.Throw(common.GetRuntime(s.ctx), err)292 }293 }294 }295}296func (s *Socket) Send(message string) {297 // NOTE: No binary message support for the time being since goja doesn't298 // support typed arrays.299 rt := common.GetRuntime(s.ctx)300 writeData := []byte(message)301 if err := s.conn.WriteMessage(websocket.TextMessage, writeData); err != nil {302 s.handleEvent("error", rt.ToValue(err))303 }304 s.msgSentTimestamps = append(s.msgSentTimestamps, time.Now())305}306func (s *Socket) Ping() {307 rt := common.GetRuntime(s.ctx)308 deadline := time.Now().Add(writeWait)309 pingID := strconv.Itoa(s.pingSendCounter)310 data := []byte(pingID)311 err := s.conn.WriteControl(websocket.PingMessage, data, deadline)312 if err != nil {313 s.handleEvent("error", rt.ToValue(err))314 return315 }316 s.pingSendTimestamps[pingID] = time.Now()317 s.pingSendCounter++318}319func (s *Socket) trackPong(pingID string) {320 pongTimestamp := time.Now()321 if _, ok := s.pingSendTimestamps[pingID]; !ok {322 // We received a pong for a ping we didn't send; ignore323 // (this shouldn't happen with a compliant server)324 return325 }326 pingTimestamp := s.pingSendTimestamps[pingID]327 s.pingTimestamps = append(s.pingTimestamps, pingDelta{pingTimestamp, pongTimestamp})328}329func (s *Socket) SetTimeout(fn goja.Callable, timeoutMs int) {330 // Starts a goroutine, blocks once on the timeout and pushes the callable331 // back to the main loop through the scheduled channel332 go func() {333 select {...

Full Screen

Full Screen

trackPong

Using AI Code Generation

copy

Full Screen

1import (2var ws = websocket.Upgrader{3}4func main() {5 http.HandleFunc("/trackPong", wsHandler)6 log.Println("Server started on :8080")7 http.ListenAndServe(":8080", nil)8}9func wsHandler(w http.ResponseWriter, r *http.Request) {10 conn, err := ws.Upgrade(w, r, nil)11 if err != nil {12 log.Println(err)13 }14 defer conn.Close()15 for {16 _, p, err := conn.ReadMessage()17 if err != nil {18 log.Println(err)19 }20 fmt.Println(string(p))21 time.Sleep(time.Second * 1)22 }23}

Full Screen

Full Screen

trackPong

Using AI Code Generation

copy

Full Screen

1func (ws *Ws) Pong() {2 ws.trackPong()3}4func (ws *Ws) Ping() {5 ws.trackPing()6}7func (ws *Ws) Close() {8 ws.trackClose()9}10func (ws *Ws) Open() {11 ws.trackOpen()12}13func (ws *Ws) Error() {14 ws.trackError()15}16func (ws *Ws) Message() {17 ws.trackMessage()18}19func (ws *Ws) Send() {20 ws.trackSend()21}22func (ws *Ws) Connect() {23 ws.trackConnect()24}25func (ws *Ws) Reconnect() {26 ws.trackReconnect()27}28func (ws *Ws) Reconnecting() {29 ws.trackReconnecting()30}31func (ws *Ws) ReconnectError() {32 ws.trackReconnectError()33}34func (ws *Ws) ReconnectFailed() {35 ws.trackReconnectFailed()36}37func (ws *Ws) ReconnectTimeout() {38 ws.trackReconnectTimeout()39}40func (ws *Ws) ReconnectAttempts() {41 ws.trackReconnectAttempts()42}43func (

Full Screen

Full Screen

trackPong

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 fmt.Println("Error in dialing to server")5 }6 defer conn.Close()7 for {8 conn.WriteControl(websocket.PongMessage, []byte("pong"), time.Now().Add(time.Second))9 time.Sleep(2 * time.Second)10 }11}

Full Screen

Full Screen

trackPong

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ws := NewWsClient()4 go ws.ReadPump()5 go ws.WritePump()6 go ws.TrackPong()7 ws.SendPing()8 time.Sleep(5 * time.Second)9 fmt.Println(ws.PongMessage)10}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful