Skip to content

Commit

Permalink
v.0.1.5
Browse files Browse the repository at this point in the history
Added:
- io.To("room1").To("room2")
- socket.Rooms()
- io.Sockets()
- namespace.Sockets()
  • Loading branch information
doquangtan committed Nov 26, 2024
1 parent ed1236c commit 55be799
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 13 deletions.
8 changes: 8 additions & 0 deletions engineio/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ const (
NOOP
)

// Code Message
// 0 "Transport unknown"
// 1 "Session ID unknown"
// 2 "Bad handshake method"
// 3 "Bad request"
// 4 "Forbidden"
// 5 "Unsupported protocol version"

func (id PacketType) String() string {
return strconv.Itoa(int(id))
}
Expand Down
12 changes: 8 additions & 4 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func socketIoRoute(app fiber.Router) {
io.OnConnection(func(socket *socketio.Socket) {
println("connect", socket.Nps, socket.Id)
socket.Join("demo")
io.To("demo").Emit("hello", socket.Id+" join us room...", "server message")
io.To("demo").Emit("test", socket.Id+" join us room...", "server message")

socket.On("test", func(event *socketio.EventPayload) {
socket.Emit("test", event.Data...)
Expand All @@ -31,13 +31,17 @@ func socketIoRoute(app fiber.Router) {
}
})

socket.On("to-room", func(event *socketio.EventPayload) {
socket.To("demo").To("demo2").Emit("test", "hello")
})

socket.On("leave-room", func(event *socketio.EventPayload) {
socket.Leave("demo")
io.To("demo").Emit("hello", socket.Id+" leave us room...", "server message")
socket.Join("demo2")
})

socket.On("room-emit", func(event *socketio.EventPayload) {
socket.To("demo").Emit("hello", socket.Id, event.Data)
socket.On("my-room", func(event *socketio.EventPayload) {
socket.Emit("my-room", socket.Rooms())
})

socket.On("disconnecting", func(event *socketio.EventPayload) {
Expand Down
2 changes: 1 addition & 1 deletion make.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ IF %func%==public (
echo Done
)

@REM make public v0.1.4
@REM make public v0.1.5
10 changes: 7 additions & 3 deletions namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
)

type Namespace struct {
name string
Name string
sockets *connections
rooms *rooms
onConnection connectionEvent
}

func newNamespace(name string) *Namespace {
return &Namespace{
name: name,
Name: name,
sockets: &connections{
conn: make(map[string]*Socket),
},
Expand Down Expand Up @@ -55,7 +55,11 @@ func (nps *Namespace) socketLeaveAllRooms(socket *Socket) {
}

func (nps *Namespace) To(room string) *Room {
return nps.rooms.create(room)
return nps.rooms.next(room)
}

func (nps *Namespace) Sockets() []*Socket {
return nps.sockets.all()
}

type namespaces struct {
Expand Down
50 changes: 45 additions & 5 deletions room.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,38 @@ import (
)

type Room struct {
name string
sockets connections
Name string
To func(room string) *Room
sockets connections
connectSockets []*Socket
}

func newRoom(name string) *Room {
return &Room{
name: name,
Name: name,
sockets: connections{
conn: make(map[string]*Socket),
},
}
}

func (room *Room) Emit(event string, agrs ...interface{}) error {
for _, socket := range room.sockets.all() {
socket.Emit(event, agrs...)
if len(room.connectSockets) > 0 {
for _, socket := range room.connectSockets {
socket.Emit(event, agrs...)
}
} else {
for _, socket := range room.sockets.all() {
socket.Emit(event, agrs...)
}
}
return nil
}

func (room *Room) Sockets() []*Socket {
return room.sockets.all()
}

type roomNames struct {
sync.RWMutex
list []string
Expand Down Expand Up @@ -81,6 +93,34 @@ func (n *rooms) create(name string) *Room {
return ret
}

func (n *rooms) next(name string, preRoom ...*Room) *Room {
n.Lock()
ret, ok := n.list[name]
if !ok {
ret = newRoom(name)
n.list[name] = ret
}
if len(preRoom) == 0 {
newRoom := newRoom(name)
newRoom.connectSockets = append(newRoom.connectSockets, ret.sockets.all()...)
ret.To = func(room string) *Room {
nextRoom := n.next(room, newRoom)
newRoom.Name += "_" + nextRoom.Name
newRoom.connectSockets = append(newRoom.connectSockets, nextRoom.sockets.all()...)
return newRoom
}
} else {
ret.To = func(room string) *Room {
nextRoom := n.next(room, preRoom[0])
preRoom[0].Name += "_" + nextRoom.Name
preRoom[0].connectSockets = append(preRoom[0].connectSockets, nextRoom.sockets.all()...)
return preRoom[0]
}
}
n.Unlock()
return ret
}

// func (n *rooms) get(name string) *Room {
// n.RLock()
// defer n.RUnlock()
Expand Down
4 changes: 4 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ func (s *Io) To(name string) *Room {
return s.Of("/").To(name)
}

func (s *Io) Sockets(name string) []*Socket {
return s.Of("/").Sockets()
}

func (s *Io) OnConnection(fn connectionEventCallback) {
s.Of("/").onConnection.set("connection", fn)
}
Expand Down
4 changes: 4 additions & 0 deletions socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (s *Socket) Disconnect() error {
return s.Conn.SetReadDeadline(time.Now())
}

func (s *Socket) Rooms() []string {
return s.rooms.all()
}

func (s *Socket) disconnect() {
s.Conn.Close()
s.Conn = nil
Expand Down

0 comments on commit 55be799

Please sign in to comment.