This commit is contained in:
ykomissarov committed 2019-08-02 17:59:51 +03:00
1 parent 48eaf48797
commit e32e55df15
20 files changed
+369 -117

No files matched your search

+41
View File
@@ -0,0 +1,41 @@
package ws
import (
socketio "github.com/googollee/go-socket.io"
"github.com/integration-system/isp-lib/structure"
"isp-config-service/cluster"
)
type Conn interface {
Id() string
Emit(event string, args ...interface{}) error
IsConfigClusterNode() bool
SetBackendDeclaration(backend structure.BackendDeclaration)
GetBackendDeclaration() *structure.BackendDeclaration
}
type wsConn struct {
conn socketio.Socket
backend *structure.BackendDeclaration
}
func (c *wsConn) Id() string {
return c.conn.Id()
}
func (c *wsConn) Emit(event string, args ...interface{}) error {
return c.conn.Emit(event, args...)
}
func (c *wsConn) IsConfigClusterNode() bool {
isCluster := c.conn.Request().URL.Query().Get(cluster.ClusterParam)
return isCluster == "true"
}
func (c *wsConn) SetBackendDeclaration(backend structure.BackendDeclaration) {
c.backend = &backend
}
func (c *wsConn) GetBackendDeclaration() *structure.BackendDeclaration {
return c.backend
}
+47 -11
View File
@@ -1,35 +1,71 @@
package ws
import (
socketio "github.com/googollee/go-socket.io"
"sync"
)
const (
idsRoom = "__id"
)
type RoomStore struct {
mu sync.RWMutex
rooms map[string]map[string]socketio.Conn
rooms map[string]map[string]Conn
}
func (s *RoomStore) Join(conn socketio.Conn, rooms ...string) {
func (s *RoomStore) GetOrJoinById(id string, new Conn) Conn {
s.mu.RLock()
var (
conn Conn
ok bool
)
idRoom, roomExist := s.rooms[idsRoom]
if roomExist {
conn, ok = idRoom[id]
}
s.mu.RUnlock()
if ok {
return conn
}
s.mu.Lock()
defer s.mu.Unlock()
idRoom, roomExist = s.rooms[idsRoom]
if roomExist {
conn, ok = idRoom[id]
}
if ok {
return conn
}
conn = new
if !roomExist {
s.rooms[idsRoom] = map[string]Conn{conn.Id(): conn}
return conn
}
idRoom[conn.Id()] = conn
return conn
}
func (s *RoomStore) Join(conn Conn, rooms ...string) {
s.mu.Lock()
defer s.mu.Unlock()
for _, room := range rooms {
if conns, ok := s.rooms[room]; ok {
conns[conn.ID()] = conn
conns[conn.Id()] = conn
} else {
s.rooms[room] = map[string]socketio.Conn{conn.ID(): conn}
s.rooms[room] = map[string]Conn{conn.Id(): conn}
}
}
}
func (s *RoomStore) Leave(conn socketio.Conn, rooms ...string) {
func (s *RoomStore) Leave(conn Conn, rooms ...string) {
s.mu.Lock()
defer s.mu.Unlock()
for _, room := range rooms {
if conns, ok := s.rooms[room]; ok {
delete(conns, conn.ID())
delete(conns, conn.Id())
if len(conns) == 0 {
delete(s.rooms, room)
}
@@ -37,15 +73,15 @@ func (s *RoomStore) Leave(conn socketio.Conn, rooms ...string) {
}
}
func (s *RoomStore) ToBroadcast(except socketio.Conn, rooms ...string) []socketio.Conn {
func (s *RoomStore) ToBroadcast(except Conn, rooms ...string) []Conn {
s.mu.RLock()
defer s.mu.RUnlock()
result := make([]socketio.Conn, 0)
result := make([]Conn, 0)
for _, room := range rooms {
if conns, ok := s.rooms[room]; ok {
for id, conn := range conns {
if id != except.ID() {
if id != except.Id() {
result = append(result, conn)
}
}
@@ -58,6 +94,6 @@ func (s *RoomStore) ToBroadcast(except socketio.Conn, rooms ...string) []socketi
func NewRoomStore() *RoomStore {
return &RoomStore{
mu: sync.RWMutex{},
rooms: make(map[string]map[string]socketio.Conn),
rooms: make(map[string]map[string]Conn),
}
}
+52 -5
View File
@@ -2,6 +2,8 @@ package ws
import (
"github.com/googollee/go-socket.io"
gosocketio "github.com/integration-system/golang-socketio"
"github.com/integration-system/isp-lib/logger"
"net/http"
)
@@ -11,7 +13,7 @@ type WebsocketServer struct {
}
func (s *WebsocketServer) Close() error {
return s.s.Close()
return nil
}
func (s *WebsocketServer) ServeHttp(w http.ResponseWriter, r *http.Request) {
@@ -22,10 +24,57 @@ func (s *WebsocketServer) Rooms() *RoomStore {
return s.roomStore
}
func (s *WebsocketServer) Broadcast(except socketio.Conn, room string, msg string, v ...interface{}) error {
func (s *WebsocketServer) OnWithAck(event string, f func(conn Conn, data []byte) string) *WebsocketServer {
must(s.s.On(event, func(conn socketio.Socket, data []byte) string {
logger.Debugf("[%s]:$s:%s", conn.Request().RemoteAddr, event, string(data))
return f(s.roomStore.GetOrJoinById(conn.Id(), &wsConn{conn: conn}), data)
}))
return s
}
func (s *WebsocketServer) On(event string, f func(conn Conn, data []byte)) *WebsocketServer {
must(s.s.On(event, func(conn socketio.Socket, data []byte) {
logger.Debugf("[%s]:$s:%s", conn.Request().RemoteAddr, event, string(data))
f(s.roomStore.GetOrJoinById(conn.Id(), &wsConn{conn: conn}), data)
}))
return s
}
func (s *WebsocketServer) OnConnect(f func(Conn)) *WebsocketServer {
must(s.s.On(gosocketio.OnConnection, func(conn socketio.Socket) {
logger.Debugf("[%s]:$s", conn.Request().RemoteAddr, gosocketio.OnConnection)
f(s.roomStore.GetOrJoinById(conn.Id(), &wsConn{conn: conn}))
}))
return s
}
func (s *WebsocketServer) OnDisconnect(f func(Conn)) *WebsocketServer {
must(s.s.On(gosocketio.OnDisconnection, func(conn socketio.Socket) {
logger.Debugf("[%s]:$s", conn.Request().RemoteAddr, gosocketio.OnDisconnection)
c := s.roomStore.GetOrJoinById(conn.Id(), &wsConn{conn: conn})
f(c)
s.roomStore.Leave(c, idsRoom)
}))
return s
}
func (s *WebsocketServer) OnError(f func(Conn, error)) *WebsocketServer {
must(s.s.On(gosocketio.OnError, func(conn socketio.Socket, err error) {
logger.Debugf("[%s]:$s:%v", conn.Request().RemoteAddr, gosocketio.OnError, err)
f(s.roomStore.GetOrJoinById(conn.Id(), &wsConn{conn: conn}), err)
}))
return s
}
func (s *WebsocketServer) Broadcast(except Conn, room string, msg string, v ...interface{}) error {
conns := s.roomStore.ToBroadcast(except, room)
for _, conn := range conns {
conn.Emit(msg, v...)
_ = conn.Emit(msg, v...)
}
return nil
}
@@ -36,8 +85,6 @@ func NewWebsocketServer() (*WebsocketServer, error) {
return nil, err
}
go server.Serve()
return &WebsocketServer{
s: server,
roomStore: NewRoomStore(),
+7
View File
@@ -0,0 +1,7 @@
package ws
func must(err error) {
if err != nil {
panic(err)
}
}