Websocket Didnt Close on socketClient.Close()


  • Webscoket is not closing connection when we fire socketClient.Close() function

    package main
    
    import (
    	"fmt"
    	"log"
    	"time"
    
    	SmartApi "github.com/angelbroking-github/smartapigo"
    	"github.com/angelbroking-github/smartapigo/websocket"
    )
    
    var socketClient *websocket.SocketClient
    
    // Triggered when any error is raised
    func onError(err error) {
    	fmt.Println("Error: ", err)
    }
    
    // Triggered when websocket connection is closed
    func onClose(code int, reason string) {
    	fmt.Println("Close: ", code, reason)
    }
    
    // Triggered when connection is established and ready to send and accept data
    func onConnect() {
    	fmt.Println("Connected")
    	err := socketClient.Subscribe()
    	if err != nil {
    		fmt.Println("err: ", err)
    	}
    }
    
    // Triggered when a message is received
    func onMessage(message []map[string]interface{}) {
    	fmt.Printf("Message Received :- %v\n", message)
    }
    
    // Triggered when reconnection is attempted which is enabled by default
    func onReconnect(attempt int, delay time.Duration) {
    	fmt.Printf("Reconnect attempt %d in %fs\n", attempt, delay.Seconds())
    }
    
    // Triggered when maximum number of reconnect attempt is made and the program is terminated
    func onNoReconnect(attempt int) {
    	fmt.Printf("Maximum no of reconnect attempt reached: %d\n", attempt)
    }
    
    func main() {
    
    	// Create New Angel Broking Client
    	ABClient := SmartApi.New("CleintCode", "Password", "APIKEY")
    
    	// User Login and Generate User Session
    	session, err := ABClient.GenerateSession()
    
    	if err != nil {
    		fmt.Println(err.Error())
    		return
    	}
    
    	//Get User Profile
    	session.UserProfile, err = ABClient.GetUserProfile()
    
    	if err != nil {
    		fmt.Println(err.Error())
    		return
    	}
    
    	socketClient = websocket.New(session.ClientCode, session.FeedToken, "nse_fo|37516&nse_fo|56278&nse_fo|57005&nse_fo|57006&nse_fo|57003&nse_fo|57004&nse_fo|56279")
    
    	// Assign callbacks
    	socketClient.OnError(onError)
    	socketClient.OnClose(onClose)
    	socketClient.OnMessage(onMessage)
    	socketClient.OnConnect(onConnect)
    	socketClient.OnReconnect(onReconnect)
    	socketClient.OnNoReconnect(onNoReconnect)
    
    	go startlivefeed()
    
    	time.Sleep(time.Second * 20)
    
    	stoplivefeed()
    
    }
    
    func startlivefeed() {
    	log.Println("Starting live feed")
    	socketClient.Serve()
    }
    
    func stoplivefeed() {
    	log.Println("Close live feed")
    	socketClient.Close()
    
    	time.Sleep(time.Second * 120)
    }
    

    result when close function is fired.

    Message Received :- [map[e:nse_fo ltp:39585.25 ltq:25 ltt:NA name:sf tk:37516]]
    Close:  1000 
    Error:  Error reading data: websocket: close 1000 (normal)
    Connected
    Message Received :- [map[e:nse_fo ltp:49.35 ltq:25 ltt:NA 
    ... Data Keeps on comming
    

    Please check and revert


  • @admin please reply