рдирдорд╕реНрддреЗ, рдореИрдВ рд╕реЛрдЪ рд░рд╣рд╛ рдерд╛ рдХрд┐ рдореИрдВ рдЧреЛрд░рд┐рд▓реНрд▓рд╛ рд╡реЗрдмрд╕реЛрдХреЗрдЯ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдХреЗ рдХрдиреЗрдХреНрд╢рди рдХреЗ 'рдХрдорд░реЗ' рдмрдирд╛рдиреЗ рдХреЗ рдмрд╛рд░реЗ рдореЗрдВ рдХреИрд╕реЗ рдЬрд╛рдЙрдВрдЧрд╛ред
рдореИрдВрдиреЗ рдПрдХ рд╕рд╛рдзрд╛рд░рдг рдЪреИрдЯ рд╕рд░реНрд╡рд░ рд╕реНрдерд╛рдкрд┐рдд рдХрд┐рдпрд╛ рд╣реИ рдЬреЛ рд╕рднреА рдХреЗ рд▓рд┐рдП рдкреНрд░рд╕рд╛рд░рд┐рдд рд╣реЛрддрд╛ рд╣реИ, рд▓реЗрдХрд┐рди рдореИрдВ рд╕реЛрдЪ рд░рд╣рд╛ рдерд╛ рдХрд┐ рдореИрдВ рдХрдорд░реЗ рдХреЛ рдХреИрд╕реЗ рдХрд╛рд░реНрдпрд╛рдиреНрд╡рд┐рдд рдХрд░реВрдВред
рдпрд╣рд╛рдВ рдЧреЛрд░рд┐рд▓реНрд▓рд╛ рдЙрджрд╛рд╣рд░рдг рдХреЗ рд╣рдм.рдЧреЛ рдЯреВ рд╕рдкреЛрд░реНрдЯ рд░реВрдо рдореЗрдВ рдПрдХ рдЕрд╕рдореНрдкреАрдбрд┐рдд рдФрд░ рдкрд░реАрдХреНрд╖рдг рди рдХрд┐рдпрд╛ рдЧрдпрд╛ рд╕рдВрд╢реЛрдзрди рд╣реИ:
type message struct {
data []byte
room string
}
type subscription struct {
conn *connection
room string
}
// hub maintains the set of active connections and broadcasts messages to the
// connections.
type hub struct {
// Registered connections.
rooms map[strng]map[*connection]bool
// Inbound messages from the connections.
broadcast chan message
// Register requests from the connections.
register chan subscription
// Unregister requests from connections.
unregister chan subscription
}
func (h *hub) run() {
for {
select {
case s := <-h.register:
connections := h.rooms[sub.rooom]
if connections == nil {
connections = make(map[*connection]bool)
h.rooms[s.room] = connections
}
connections[s.conn] = true
case s := <-h.unregister:
connections := h.rooms[s.rooom]
if connections != nil {
if _, ok := connections[s.conn]; ok {
delete(connections, s.conn)
close(s.conn.send)
if len(connections) == 0 {
delete(h.rooms, s.room)
}
}
}
case m := <-h.broadcast:
connections := h.rooms[m.rooom]
for c := range h.connections {
select {
case c.send <- m.data:
default:
close(c.send)
delete(h.connections, c)
if len(connections) == 0 {
delete(h.rooms, m.room)
}
}
}
}
}
}
рдХреНрд╡реЗрд░реА рд╕реНрдЯреНрд░рд┐рдВрдЧ рдпрд╛ рд╕рдВрджреЗрд╢ рд╕реЗ рдХрдорд░реЗ рдХрд╛ рдирд╛рдо рдкреНрд░рд╛рдкреНрдд рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдХрдиреЗрдХреНрд╢рди рдкреНрд░рдХрд╛рд░ рдХреЛ рд╕рдВрд╢реЛрдзрд┐рдд рдХрд░реЗрдВ рдФрд░ рд╣рдм рдХреЗ рдЪреИрдирд▓реЛрдВ рдХреЛ рднреЗрдЬреЗ рдЧрдП рд╕рднреА рдорд╛рдиреЛрдВ рдореЗрдВ рдЙрд╕ рдХрдорд░реЗ рдХрд╛ рдирд╛рдо рд╢рд╛рдорд┐рд▓ рдХрд░реЗрдВред
рдореЗрд░реЗ рдкрд╛рд╕ рдПрдХ рд╕рдВрдкреВрд░реНрдг рдЙрджрд╛рд╣рд░рдг рд▓рд┐рдЦрдиреЗ рдФрд░ рдкрд░реАрдХреНрд╖рдг рдХрд░рдиреЗ рдХрд╛ рд╕рдордп рдирд╣реАрдВ рд╣реИред рдЙрдореНрдореАрдж рд╣реИ рдХрд┐ рдпрд╣ рдЖрдкрдХреЛ рдЖрд░рдВрдн рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдкрд░реНрдпрд╛рдкреНрдд рд╣реИред
рд╢реБрдХреНрд░рд┐рдпрд╛, рдЗрд╕рд╕реЗ рдХрд╛рдлрд╝реА рдорджрдж рдорд┐рд▓рддреА рд╣реИред рдЙрд▓реНрд▓реЗрдЦ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдПрдХ рдЙрджрд╛рд╣рд░рдг рд╣реЛрдирд╛ рдмрд╣реБрдд рдЕрдЪреНрдЫрд╛ рд╣реИред
рдЗрд╕реЗ рдХрд╛рдо рдХрд░рдиреЗ рдХреА рдХреЛрд╢рд┐рд╢ рдХрд░рдиреЗ рд╡рд╛рд▓реЗ рдХрд┐рд╕реА рднреА рд╡реНрдпрдХреНрддрд┐ рдХреЗ рд▓рд┐рдП, рдореИрдВ рдЖрдЦрд┐рд░рдХрд╛рд░ рдХреБрдЫ рдареАрдХ рд╕реЗ рдХрд╛рдо рдХрд░рдиреЗ рдореЗрдВ рдХрд╛рдордпрд╛рдм рд░рд╣рд╛ред рдЖрд╢рд╛ рд╣реИ рдХрд┐ рдпрд╣ рдХрд┐рд╕реА рдХреА рднреА рдорджрдж рдХрд░рддрд╛ рд╣реИ рдЬреЛ рдореЗрд░реЗ рдЬреИрд╕рд╛ рд╣реА рдЕрдЯрдХрд╛ рд╣реБрдЖ рдФрд░ рдирд┐рд░рд╛рд╢ рд╣реИ:
conn.go:
package main
import (
"log"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
)
const (
// Time allowed to write a message to the peer.
writeWait = 10 * time.Second
// Time allowed to read the next pong message from the peer.
pongWait = 60 * time.Second
// Send pings to peer with this period. Must be less than pongWait.
pingPeriod = (pongWait * 9) / 10
// Maximum message size allowed from peer.
maxMessageSize = 512
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
// connection is an middleman between the websocket connection and the hub.
type connection struct {
// The websocket connection.
ws *websocket.Conn
// Buffered channel of outbound messages.
send chan []byte
}
// readPump pumps messages from the websocket connection to the hub.
func (s subscription) readPump() {
c := s.conn
defer func() {
h.unregister <- s
c.ws.Close()
}()
c.ws.SetReadLimit(maxMessageSize)
c.ws.SetReadDeadline(time.Now().Add(pongWait))
c.ws.SetPongHandler(func(string) error { c.ws.SetReadDeadline(time.Now().Add(pongWait)); return nil })
for {
_, msg, err := c.ws.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
log.Printf("error: %v", err)
}
break
}
m := message{msg, s.room}
h.broadcast <- m
}
}
// write writes a message with the given message type and payload.
func (c *connection) write(mt int, payload []byte) error {
c.ws.SetWriteDeadline(time.Now().Add(writeWait))
return c.ws.WriteMessage(mt, payload)
}
// writePump pumps messages from the hub to the websocket connection.
func (s *subscription) writePump() {
c := s.conn
ticker := time.NewTicker(pingPeriod)
defer func() {
ticker.Stop()
c.ws.Close()
}()
for {
select {
case message, ok := <-c.send:
if !ok {
c.write(websocket.CloseMessage, []byte{})
return
}
if err := c.write(websocket.TextMessage, message); err != nil {
return
}
case <-ticker.C:
if err := c.write(websocket.PingMessage, []byte{}); err != nil {
return
}
}
}
}
// serveWs handles websocket requests from the peer.
func serveWs(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
vars := mux.Vars(r)
log.Println(vars["room"])
if err != nil {
log.Println(err)
return
}
c := &connection{send: make(chan []byte, 256), ws: ws}
s := subscription{c, vars["room"]}
h.register <- s
go s.writePump()
s.readPump()
}
рд╣рдм.рдЧреЛ:
package main
type message struct {
data []byte
room string
}
type subscription struct {
conn *connection
room string
}
// hub maintains the set of active connections and broadcasts messages to the
// connections.
type hub struct {
// Registered connections.
rooms map[string]map[*connection]bool
// Inbound messages from the connections.
broadcast chan message
// Register requests from the connections.
register chan subscription
// Unregister requests from connections.
unregister chan subscription
}
var h = hub{
broadcast: make(chan message),
register: make(chan subscription),
unregister: make(chan subscription),
rooms: make(map[string]map[*connection]bool),
}
func (h *hub) run() {
for {
select {
case s := <-h.register:
connections := h.rooms[s.room]
if connections == nil {
connections = make(map[*connection]bool)
h.rooms[s.room] = connections
}
h.rooms[s.room][s.conn] = true
case s := <-h.unregister:
connections := h.rooms[s.room]
if connections != nil {
if _, ok := connections[s.conn]; ok {
delete(connections, s.conn)
close(s.conn.send)
if len(connections) == 0 {
delete(h.rooms, s.room)
}
}
}
case m := <-h.broadcast:
connections := h.rooms[m.room]
for c := range connections {
select {
case c.send <- m.data:
default:
close(c.send)
delete(connections, c)
if len(connections) == 0 {
delete(h.rooms, m.room)
}
}
}
}
}
}
рд╕рдмрд╕реЗ рдЙрдкрдпреЛрдЧреА рдЯрд┐рдкреНрдкрдгреА
рдЗрд╕реЗ рдХрд╛рдо рдХрд░рдиреЗ рдХреА рдХреЛрд╢рд┐рд╢ рдХрд░рдиреЗ рд╡рд╛рд▓реЗ рдХрд┐рд╕реА рднреА рд╡реНрдпрдХреНрддрд┐ рдХреЗ рд▓рд┐рдП, рдореИрдВ рдЖрдЦрд┐рд░рдХрд╛рд░ рдХреБрдЫ рдареАрдХ рд╕реЗ рдХрд╛рдо рдХрд░рдиреЗ рдореЗрдВ рдХрд╛рдордпрд╛рдм рд░рд╣рд╛ред рдЖрд╢рд╛ рд╣реИ рдХрд┐ рдпрд╣ рдХрд┐рд╕реА рдХреА рднреА рдорджрдж рдХрд░рддрд╛ рд╣реИ рдЬреЛ рдореЗрд░реЗ рдЬреИрд╕рд╛ рд╣реА рдЕрдЯрдХрд╛ рд╣реБрдЖ рдФрд░ рдирд┐рд░рд╛рд╢ рд╣реИ:
conn.go:
рд╣рдм.рдЧреЛ: