Closed Anan7Codes closed 10 months ago
@Anan7Codes
Please check https://github.com/antoniodipinto/ikisocket/blob/master/ikisocket.go#L415
The kws.Close() doesn't need params, because it's part of the socket struct (kws) and basically will close itself.
So in my use case, I have a channel b/w my user's client and an iot device. If another user tries to establish a connection to this device, I need to end the previous user's session(socket connection) and I'm using redis pubsub as well. So technically would be nice if I could close a particular connection by passing the connect string/uuid.
Create a Key:Value Object where to store the ID of the connection and the kws instance as value. When you need to drop a specific connection, just retrieve the connection from there and Close()
Below are both the functions I've used right now. So with a certain IoT device, if a user is connected, and if another user tries to connect to it, I want to disconnect the user connected in the beginning. No two users can be connected at the same time and that is why I want to disconnect the session of the first user so they can be notified. But right now, I am not able to able to close that connection while persisting the new one.
func DisconnectWebsocket(userId string, uuid string, kws *ikisocket.Websocket) {
log.Println("Deleted from Redis: ", userId)
log.Println("Closed: ", uuid)
kws.SetUUID(uuid)
kws.Close()
err := utils.DeleteFromRedis(userId)
if err != nil {
fmt.Println("Error deleting value from Redis:", err)
return
}
}
func ConnectWebsocket() fiber.Handler {
return ikisocket.New(func(kws *ikisocket.Websocket) {
userId := kws.Locals("userId").(string)
log.Println("User ID: ", userId)
log.Println("KWS ID: ", kws.UUID)
isActiveSession := kws.Locals("isActiveSession").(bool)
log.Println("Is Active Session: ", isActiveSession)
err := utils.SetToRedis(userId, kws.UUID)
if err != nil {
fmt.Println("Error setting value to Redis:", err)
return
}
if isActiveSession {
DisconnectWebsocket(activeUserId, socketClientId, kws)
}
// Every websocket connection has an optional session key => value storage
kws.SetAttribute("user_id", userId)
msg := Message{
Data: "Welcome Message",
Event: "connected",
}
// Convert message to JSON string
jsonMsg, err := json.Marshal(msg)
if err != nil {
fmt.Println("Error marshalling message to JSON:", err)
return
}
//Write welcome message
kws.Emit(jsonMsg, ikisocket.TextMessage)
})
}
This is not an issue related to the library itself.
Ikisocket provides a way of closing the connection, you're asking a solution to a software that you're developing not on the library itself. I will close the issue with this comment so we don't use the issue tab for non-issues. Thanks
Can you atleast tell me how I can retrieve a connection or where I can this discussion up further?
Hi, I can see there is a close method but no connection string/uuid is being passed. I would like to know how I can disconnect a particular connection 🤔