create intial db, models, websocket and architecture.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package websocket
|
||||
|
||||
import "github.com/gorilla/websocket"
|
||||
|
||||
type Client struct {
|
||||
conn *websocket.Conn
|
||||
room *Room
|
||||
}
|
||||
|
||||
type Room struct {
|
||||
id string
|
||||
clients map[*Client]bool
|
||||
}
|
||||
|
||||
func NewRoom(id string) *Room {
|
||||
return &Room{
|
||||
id: id,
|
||||
clients: make(map[*Client]bool),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Room) Register(client *Client) {
|
||||
r.clients[client] = true
|
||||
}
|
||||
|
||||
func (r *Room) Unregister(client *Client) {
|
||||
delete(r.clients, client)
|
||||
client.conn.Close()
|
||||
}
|
||||
|
||||
func (r *Room) Broadcast(message []byte) {
|
||||
for client := range r.clients {
|
||||
err := client.conn.WriteMessage(websocket.TextMessage, message)
|
||||
if err != nil {
|
||||
r.Unregister(client)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user