Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Connections method in websocket #284

Merged
merged 1 commit into from
Jan 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ws/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ type WsServer interface {
// Addr gives the address on which the server is listening, useful if, for
// example, the port is system-defined (set to 0).
Addr() *net.TCPAddr
// Connections retrieves a WebSocket connection by its unique identifier.
// If a connection with the given ID exists, it returns the corresponding WebSocket instance.
// If no connection is found with the specified ID, it returns nil.
Connections(websocketId string) *WebSocket
}

// Default implementation of a Websocket server.
Expand Down Expand Up @@ -371,6 +375,10 @@ func (server *Server) Addr() *net.TCPAddr {
return server.addr
}

func (server *Server) Connections(websocketId string) *WebSocket {
return server.connections[websocketId]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be thread-safe? Could happen you have a connection what is being removed at the same time and you get a stale connection map

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, fixed here

}

func (server *Server) AddHttpHandler(listenPath string, handler func(w http.ResponseWriter, r *http.Request)) {
server.httpHandler.HandleFunc(listenPath, handler)
}
Expand Down
Loading