https://github.com/gobackpack/websocket
WIP: Websockets hub
https://github.com/gobackpack/websocket
golang websockets
Last synced: over 1 year ago
JSON representation
WIP: Websockets hub
- Host: GitHub
- URL: https://github.com/gobackpack/websocket
- Owner: gobackpack
- Created: 2021-04-21T08:17:53.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2023-09-15T22:14:27.000Z (almost 3 years ago)
- Last Synced: 2025-01-24T15:36:30.520Z (over 1 year ago)
- Topics: golang, websockets
- Language: Go
- Homepage:
- Size: 103 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README

Work in progress...
Usage
```go
// initialize hub and start listening for connections
hub := websocket.NewHub()
hubCtx, hubCancel := context.WithCancel(context.Background())
hubFinished := hub.ListenForConnections(hubCtx)
// create client and establish connection with ws hub
client, err := hub.EstablishGorillaWsConnection(c.Writer, c.Request, groupId, connId)
if err != nil {
logrus.Errorf("failed to establish connection with groupId -> %s: %s", groupId, err)
return
}
clientCtx, clientCancel := context.WithCancel(hubCtx)
clientFinished := client.ReadMessages(clientCtx)
// handle messages
go func (clientCancel context.CancelFunc, client *websocket.Client) {
defer clientCancel()
for {
select {
case msg := <-client.OnMessage:
logrus.Infof("client %s received message: %s", client.ConnectionId, msg)
go hub.SendToGroup(groupId, msg)
case err := <-client.OnError:
logrus.Errorf("client %s received error: %s", client.ConnectionId, err)
case err = <-client.LostConnection:
hub.DisconnectFromGroup(client.GroupId, client.ConnectionId)
return
}
}
}(clientCancel, client)
<-clientFinished
// send message
go hub.SendToGroup(groupId, []byte("message to group"))
go hub.SendToAllGroups([]byte("message to all groups"))
go hub.SendToConnectionId(groupId, connectionId, []byte("message to connection"))
go hub.SendToOthersInGroup(groupId, client.ConnectionId, []byte("message to all connections from my group except myself"))
// disconnect
hub.DisconnectFromGroup(groupId, connectionId)
// close
hubCancel()
<-hubFinished
```
#### Todo
* Make sure the following are thread-safe:
```go
client.read()
client.write()
hub.Groups
```